gpt4 book ai didi

图形QLError : There can be only one fragment named

转载 作者:行者123 更新时间:2023-12-05 06:14:07 24 4
gpt4 key购买 nike

在使用 graphql-react 构建的客户端应用程序上,

graphQL 查询字符串由多个片段组成,如下所示:

const fragmentAddress = `
fragment address on Address {
id
number
street
code
city
}
`

const fragmentOffice = `
fragment office on Office {
id
address {
...address
}
}

${fragmentAddress}
`

const User = `
query User($id: ID) {
address {
...address
}
office {
...office
}
}

${fragmentAddress}
${fragmentOffice}
`

User 的查询返回此错误:GraphQLError: There can be only one fragment named address

我看到了graphql-tag具有去重功能,但不能返回字符串。

如何从 graphql 查询字符串中删除重复片段?

最佳答案

我只是found the answer :

使用 graphql-tag 进行去重,使用 graphql/language/printer 转换回字符串:

import gql from 'graphql-tag'
import { print } from 'graphql/language/printer'

const fragmentAddress = gql`
fragment address on Address {
id
number
street
code
city
}
`

const fragmentOffice = gql`
fragment office on Office {
id
address {
...address
}
}

${fragmentAddress}
`

const User = gql`
query User($id: ID) {
address {
...address
}
office {
...office
}
}

${fragmentAddress}
${fragmentOffice}
`

const queryString = print(query)

关于图形QLError : There can be only one fragment named,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63034980/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com