微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

如何从生成的架构中获取简单的graphql变异查询?

如何解决如何从生成的架构中获取简单的graphql变异查询?

我正在使用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 }
  })

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

解决方法

如果我对您的理解正确,那么AddDataDocumentDocumentNode对象(gql调用的返回值),您想从中以字符串形式返回查询。是吗?

如果是这样,请尝试:

import { print } from 'graphql'

...

request(app.getHttpServer())
  .post('/graphql')
  .send({
    operationName: null,query: print(AddDataDocument),variables: { input: addDataInput }
  })

几天前,我也遇到了类似的问题(问题的“如何将已解析的文档对象放回字符串中”)。

这是这个想法的来源:

https://github.com/apollographql/graphql-tag/issues/144

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。