【问题标题】:Adding an argument to a GraphQL request向 GraphQL 请求添加参数
【发布时间】:2017-01-05 20:35:34
【问题描述】:

我正在使用React Starter Kit,我正在尝试将一个字符串与查询一起作为参数传递,但没有任何效果。我尝试了以下方法:

export default {

  path: '/',

  async action() {
    const resp = await fetch('/graphql', {
      method: 'post',
      headers: {
        Accept: 'application/json',
        'Content-Type': 'application/json',
      },
      body: JSON.stringify({
        query: `{
          posts {
            id,
            title,
            content,
            date
          },
          page(slug: 'homepage'){
            id,
            date,
            modified,
            slug,
            type,
            title,
            content,
            excerpt,
            author
          }
        }`,
      }),
      credentials: 'include',
    });
    const { data } = await resp.json();
    console.log('in home/index');
    if (!data || !data.posts) throw new Error('Failed to load the homepage.');
    return {
      title: 'React Starter Kit',
      component: <Layout><Home posts={data.posts} page={data.page} /></Layout>,
    };
  },

};

但每次我包含括号时它都会失败...传递参数的正确方式是什么?

【问题讨论】:

  • “失败”是什么意思,“包含括号”是什么意思?无论如何,您可能应该使用变量:graphql.org/learn/queries/#variables
  • 嘿@stubailo 一直是您作品的忠实粉丝,感谢您的回复。事实证明,我的架构设置不正确,无法接受参数。当我所说的“parens”是指一旦我将它们包含在上面的代码中,我就会得到 thought 是语法错误,但这只是架构没有正确设置。

标签: graphql react-starter-kit


【解决方案1】:

解决这个问题的方法是在模式的最终查询类型中正确设置参数,如下所示:

type Page{
  id: Int
  slug: String
  title: String
  content: String
}

type Query{
  page(slug: String): Page 
}

schema {
  query: Query
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2013-06-18
    • 2016-04-13
    • 2015-07-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-19
    相关资源
    最近更新 更多