![](/img/trans.png)
[英]Apollo GraphQL - How to get data recorded by mutation from the cache store?
[英]How to get simple graphql mutation query from generated schema?
我正在使用 graphql 代码生成器来获取生成的文件:
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 }
})
代码生成.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
object( gql
调用的返回值),您希望从中以字符串形式返回查询。 是吗?
如果是这样试试这个:
import { print } from 'graphql'
...
request(app.getHttpServer())
.post('/graphql')
.send({
operationName: null,
query: print(AddDataDocument),
variables: { input: addDataInput }
})
几天前我遇到了类似的问题(问题的“如何将已解析的文档 object 恢复为字符串”部分)。
这是这个想法的来源:
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.