我的 IDE(带有 JS GraphQL 的 Phpstorm)给了我架构的标题错误。
我是 GraphQL 新手,如果实际查询操作仅在根级别发生突变,那么查询应该设置为什么?
下面是从 (Shopify) GraphQL API 教程中摘取的实际查询。我正在复制下面的本地架构定义,试图适应其形状。
正如您所看到的,查询完全嵌套在突变中,因此我不知道根级别的查询定义应该有什么。
// graphql.ts
import "isomorphic-fetch";
const buildPricingPlanQuery = (redirectUrl: string) => `mutation {
appSubscribeCreate(
name : "Plan 1"
returnUrl : "${redirectUrl}"
test : true
lineItems : [
{
plan : {
appUsagePricingDetails : {
cappedAmount : {
amount : 10
, currencyCode : USD
}
terms : "Up to 50 products"
}
}
}
{
plan : {
appRecurringPricingDetails : {
price : {
amount : 10
, currencyCode : USD
}
terms : "some recurring terms"
}
}
}
]
)
{
userErrors {
field
message
}
confirmationUrl
appSubscription {
id
}
}
}`;
export const requestSubscriptionUrl = async (ctx: any, accessToken: string, shopDomain: string) => {
const requestUrl = `https://${shopDomain}/admin/api/2019-10/graphql.json`;
const response = await fetch(requestUrl, {
method : 'post'
, headers : {
'content-type' : "application/json"
, 'x-shopify-access-token' : accessToken
},
body : JSON.stringify({query: buildPricingPlanQuery(`https://${shopDomain}`)})
});
const responseBody = await response.json();
const confirmationUrl = responseBody
.data
.appSubscriptionCreate
.confirmationUrl;
return confirmationUrl;
};
Run Code Online (Sandbox Code Playgroud)
// pricingSchema.graphql
# ------------ Minor Types
enum CurrencyCode {
USD
EUR
JPY
}
type cappedAmount {
amount: Int
currencyCode : CurrencyCode
}
type appUsagePricingDetails {
cappedAmount: cappedAmount
}
input PlanInput {
appUsagePricingDetails: cappedAmount
terms: String
}
type userErrors {
field: String
message: String
}
type appSubscription {
id: Int
}
# ------------ Major Type and Schema definition
type PricingPlan {
appSubscribeCreate(
name: String!
returnUrl: String!
test: Boolean
lineItems: [PlanInput!]!
): String
userErrors: userErrors
confirmationUrl: String
appSubscription: appSubscription
}
schema {
mutation: PricingPlan
}
Run Code Online (Sandbox Code Playgroud)
您看到的错误是指GraphQL 规范的这一规定:
必须提供查询根操作类型,并且必须是对象类型。
已经有几个 建议来删除此限制,但截至最新(2018 年 6 月)规范,如果没有查询类型,架构将被视为无效。该规范还规定对象类型(包括查询)不能为空。
我的建议:只需添加一个简单的查询类型,例如
type Query {
ping: String @deprecated(reason: "/sf/ask/4190825971/")
}
Run Code Online (Sandbox Code Playgroud)
如果规范更新,您可以稍后将其删除:)
归档时间: |
|
查看次数: |
4424 次 |
最近记录: |