【问题标题】:How do you format your body in a fetch request for a GraphQL mutation?如何在 GraphQL 突变的 fetch 请求中格式化你的 body?
【发布时间】:2020-04-01 01:53:39
【问题描述】:

我是第一次尝试 GraphQL,我正在尝试 POST 一些数据并在 MongoDB 数据库中创建一个新文档。

我已经定义了一个“办公室”模型。这是文档的样子:

{
  fax: 4161234567,
  image: {
    full: 'http://placehold.it/1400x800',
    large: 'http://placehold.it/1000x600',
    medium: 'http://placehold.it/700x400',
    small: 'http://placehold.it/400x200'
  },
  location: {
    address: '123 Street Name',
    city: 'Toronto',
    country: 'Canada',
    countryCode: 'CA',
    state: 'ON',
    suite: null,
    zip: 'H6L 1C8'
  },
  phone: 4161234567,
  slug: 'office-name',
  title: 'Office Name' 
}

我在服务器上使用 Node 和 Express。这是我的突变:

const schema = buildSchema(`
  type Image {
    full: String
    large: String
    medium: String
    small: String
  }

  type Location {
    address: String
    city: String
    country: String
    countryCode: String
    state: String
    suite: String
    zip: String
  }

  input ImageInput {
    full: String
    large: String
    medium: String
    small: String
  }

  input LocationInput {
    address: String
    city: String
    country: String
    countryCode: String
    state: String
    suite: String
    zip: String
  }

  type Office {
    _id: String
    fax: Float
    image: Image
    location: Location
    phone: Float
    slug: String
    title: String
  }

  type Mutation {
    createOffice(fax: Float, image: ImageInput, location: LocationInput, phone: Float, slug: String, title: String): Office
  }
`);

客户端,我有这个:

fetch('http://localhost:4300/graphql', {
  method: 'POST',
  headers: {
    'Content-Type': 'application/json'
  },
  body: JSON.stringify({
    query: 'mutation { createOffice(fax: 5041234567, image: {full: "http://placehold.it/1400x800", large: "http://placehold.it/1000x600", medium: "http://placehold.it/700x400", small: "http://placehold.it/400x200"}, location: {address: "456 Wilcox Ave.", city: "Montreal", country: "Canada", countryCode: "CA", state: "QC", suite: 2300, zip: "H3A 0A8"}, phone: 5047654321, slug: "my-office", title: "My Office" { phone }  }'
  }),
})
.then((res) => {
  return res.json();
})
.then((res) => {
  console.log(res.data);
});

我收到以下错误:

Syntax Error: Expected Name, found {"

我认为这是我的语法问题:

body: JSON.stringify({
  query: 'mutation { createOffice(fax: 5041234567, image: {full: "http://placehold.it/1400x800", large: "http://placehold.it/1000x600", medium: "http://placehold.it/700x400", small: "http://placehold.it/400x200"}, location: {address: "456 Wilcox Ave.", city: "Montreal", country: "Canada", countryCode: "CA", state: "QC", suite: 2300, zip: "H3A 0A8"}, phone: 5047654321, slug: "my-office", title: "My Office" { phone }  }'
})

我做错了什么?

【问题讨论】:

  • 由于您刚刚开始,请注意您通常也应该避免使用buildSchema,即使官方文档在他们的示例中使用它。详情here.
  • @DanielRearden 我理解使用graphql.GraphQLObjectType 代替buildSchema 但输入的等价物是什么?我在graphql.GraphQLObjectInput 上没有看到任何文档。
  • 没关系。我找到了GraphQLInputObjectType。命名约定不是很直观,但我现在明白了。

标签: graphql fetch graphql-mutation


【解决方案1】:

您在“我的办公室”之后缺少一个右括号,以指示您的论点列表的结尾。

假设您为 HTTP 服务器使用 apollo-serverexpress-graphql 或类似库,您应该有一个 GraphiQL 或 GraphQL Playground 接口,您可以使用它来测试您的查询。如果没有,您也可以使用独立客户端。无论哪种情况,您都可以在具有自动完成和语法高亮功能的编辑器中编写和测试查询,从而更容易捕获此类语法错误。

您可能还会发现在前端编写查询时使用template literals 很有帮助,以避免将所有内容放在一行中。

【讨论】:

  • 哇,多么愚蠢的错误。有时您只需要第二双眼睛,谢谢!我对 GraphQL 很陌生,所以我不确定这是否是我的语法。所有的括号和方括号都可能有点棘手。也感谢您的提示。我正在使用express-graphql,但我在 React 应用程序中编写查询以尽可能接近地模拟真实世界的示例。由于查询时间过长,很难发现任何语法问题。
  • 我仍然会先在 GraphiQL 的编辑器中编写查询,然后将其复制并粘贴到您的前端代码中。它将使您的生活在多个层面上更轻松。唯一不需要这样做的情况是,如果您有一个提供相同功能的编辑器插件。
猜你喜欢
  • 2019-07-26
  • 2019-02-05
  • 2017-11-07
  • 2022-08-13
  • 1970-01-01
  • 1970-01-01
  • 2020-03-03
  • 2019-02-08
  • 2022-01-12
相关资源
最近更新 更多