[英]How to get simple graphql mutation query from generated schema?
I'm using the graphql code generator to get my generated file:我正在使用 graphql 代码生成器来获取生成的文件:
npx graphql-codegen --config libs/codegen.yml
In this file I do have在这个文件中我有
export const AddDataDocument = gql`
mutation addData($input: AddDataInput!) {
addData(input: $input) {
id
}
}`;
which gives me the gql document.这给了我 gql 文档。
In my tests (using supertest
), I'm doing:在我的测试中(使用supertest
),我在做:
const query = `
mutation addData($input: AddDataInput!) {
addData(input: $input) {
id
}
}`
request(app.getHttpServer())
.post('/graphql')
.send({
operationName: null,
query,
variables: { input: addDataInput }
})
I do not want to write the mutation manually.我不想手动编写突变。 I would like to use my generated schema file.我想使用我生成的模式文件。 But unfortunatly there is a gql defined, like mentioned at the beginning of this post.但不幸的是,定义了一个 gql,就像本文开头提到的那样。
Is it possible to generate something to be used in my send()
?是否可以生成要在我的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代码生成.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
If I understood you correctly, AddDataDocument
is a DocumentNode
object (the return value of the gql
invocation) and you want to get back the query as a string from it.如果我理解正确的话, AddDataDocument
是一个DocumentNode
object( gql
调用的返回值),您希望从中以字符串形式返回查询。 Is that it?是吗?
If so try this:如果是这样试试这个:
import { print } from 'graphql'
...
request(app.getHttpServer())
.post('/graphql')
.send({
operationName: null,
query: print(AddDataDocument),
variables: { input: addDataInput }
})
I've faced a similar problem a couple of days ago (the "how do I get the parsed document object back into a string" part of the question).几天前我遇到了类似的问题(问题的“如何将已解析的文档 object 恢复为字符串”部分)。
Here's where the idea came from:这是这个想法的来源:
https://github.com/apollographql/graphql-tag/issues/144 https://github.com/apollographql/graphql-tag/issues/144
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.