2016-12-28 67 views
1

我正在使用React Starter Kit,我试图将查询作为参数传递给一个字符串,但没有任何效果。我曾尝试以下:向GraphQL请求添加参数

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

 
};

但它不能每次我包括括号...什么是沿着一个参数传递正确方式?

+0

你说的“失败”和你是什么意思意思“包括括号“?无论如何,这可能是你应该使用变量:http://graphql.org/learn/queries/#variables – stubailo

+0

嘿@stubailo是你的工作的长期粉丝,感谢您的答复。事实证明,我的模式设置不适用于接受参数。当我的意思是“parens”时,当我将它们包含在上面的代码中时,我会得到*认为*是语法错误,但只是模式没有正确设置。 – j0e

回答

0

这个问题的解决方法是正确设置参数在最后的查询类型的模式,就像这样:

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

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

schema { 
    query: Query 
}