向GraphQL请求添加参数

时间:2016-12-28 00:58:01

标签: graphql react-starter-kit

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

};

但每次我加入parens时它都会失败...... 传递参数的方法是什么?

1 个答案:

答案 0 :(得分:0)

此问题的解决方案是在架构中的最终Query类型中正确设置参数,如下所示:

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

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

schema {
  query: Query
}
相关问题