2016-12-28 184 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 
}