1

I am using React Starter Kit and I'm trying to pass a string along with the query as an argument but nothing works. I have tried the following:

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>,
    };
  },

};

But it fails every time I include the parens... what is the right way to pass along an argument?

j0e
  • 1,441
  • 1
  • 15
  • 17
  • What do you mean by "it fails" and what do you mean by "include parens"? Anyway it's likely you should be using variables: http://graphql.org/learn/queries/#variables – stubailo Jan 05 '17 at 08:18
  • hey @stubailo been a long-time fan of your work, thanks for your reply. As it turns out my schema setup wasn't correct to accept arguments. When I meant by "parens" was that as soon as I included them in the code above I would get what *thought* was a syntax error, but it was just that the schema wasn't properly setup. – j0e Jan 05 '17 at 12:28

1 Answers1

0

The solution to this problem is to correctly setup arguments in the final Query type in the schema, like so:

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

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

schema {
  query: Query
}
j0e
  • 1,441
  • 1
  • 15
  • 17