问题描述
npx graphql-codegen --config libs/codegen.yml
export const AddDataDocument = gql`
mutation addData($input: AddDataInput!) {
addData(input: $input) {
id
}
}`;
这给了我gql文档。
在测试中(使用supertest
),我在做
const query = `
mutation addData($input: AddDataInput!) {
addData(input: $input) {
id
}
}`
request(app.getHttpServer())
.post('/graphql')
.send({
operationName: null,query,variables: { input: addDataInput }
})
我不想手动编写变异。我想使用我生成的模式文件。但不幸的是,已经定义了一个gql,就像在本文开头提到的那样。
是否可以生成一些要在我的send()
中使用的东西?
request(app.getHttpServer())
.post('/graphql')
.send({
operationName: null,query: AddDataDocument,// <-- but this is not working as it is a gql document
variables: { input: addDataInput }
})
codegen.yml
overwrite: true
schema: "apps/backend/src/app/**/*.graphql"
generates:
libs/generated/generated.ts:
documents: "libs/**/*.graphql"
plugins:
- "typescript"
- "typescript-operations"
- "typescript-react-apollo"
config:
withHooks: true
withComponent: false
withHOC: false
解决方法
如果我对您的理解正确,那么AddDataDocument
是DocumentNode
对象(gql
调用的返回值),您想从中以字符串形式返回查询。是吗?
如果是这样,请尝试:
import { print } from 'graphql'
...
request(app.getHttpServer())
.post('/graphql')
.send({
operationName: null,query: print(AddDataDocument),variables: { input: addDataInput }
})
几天前,我也遇到了类似的问题(问题的“如何将已解析的文档对象放回字符串中”)。
这是这个想法的来源: