将 graphql 查询 .gql 文件导入纯 Javascript 和 运行 一个 axios 调用
Import graphql query .gql file into plain Javascript and run an axios call
我有一个简单的 axios 调用,我想调用它来获取一些 graphql 数据。
我还有一个 .gql
文件,我想将其导入以用作查询:
pages.gql
{
entries(section: [pages]) {
uri
slug
}
}
现在我想在我的其他文件中导入:
import query from './queries/pages.gql'
稍后进行 axios 调用:
const routes = await axios.post(
endpoint,
{ query },
{ headers: { Authorization: `Bearer ${token}` } }
)
.then(result => {
console.log('result: ', result)
})
.catch(error => {
console.log('error: ', error)
})
但是每当我这样做时,我都会得到这个错误:
missing ) after argument list 09:55:25
entries(section: [pages]) {
^^^^^^^
SyntaxError: missing ) after argument list
所以我不能只导入 gql 文件。
我已导入 graphql-tag
(import gql from 'graphql-tag'
),所以我可以这样做:
const QUERY = gql`
${query}
`
但首先我必须导入 gql 文件,对吗?
任何帮助将不胜感激...
干杯
---- 编辑 2019-08-28:----
好的,另一种情况:
我有一个非常有用的 axios 调用:
await axios
.post(
endpoint,
{ query: `query Page($slug: String!) {
entries(section: [pages], slug: $slug) {
slug
title
}
}`,
variables: { slug: params.slug }
},
{ headers: { Authorization: `Bearer ${env.GRAPHQL_TOKEN}` } }
).then(result => {
console.log('result: ', result)
})
.catch(error => {
console.log('error: ', error)
})
我还有一个 page.gql 文件,内容完全相同:
query Page($slug: String!) {
entries(section: [pages], slug: $slug) {
slug
title
}
}
我发现像这样导入 page.gql 文件
import page from '~/apollo/queries/page'
将导入这种结构的东西:
page: { 15:16:40
kind: 'Document',
definitions: [...],
loc: {
start: 0,
end: 181,
source: {
body: 'query Page($slug: String!) {\n ' +
'entries(section: [pages], slug: $slug) {\n ' +
'slug\n title\n }\n }\n}\n',
name: 'GraphQL request',
locationOffset: [Object]
}
},
Page: {...}
}
所以要在我的查询中使用 page.gql 我必须使用这个 page.loc.source.body
:
await axios
.post(
endpoint,
{ query: page.loc.source.body,
variables: { slug: params.slug }
},
{ headers: { Authorization: `Bearer ${env.GRAPHQL_TOKEN}` } }
).then(result => {
console.log('result: ', result)
})
.catch(error => {
console.log('error: ', error)
})
还有比这更好的方法吗?
graphql 标签模板文字(gql`..`
)将 graphql 查询解析为 AST(抽象语法树;进一步阅读:https://www.contentful.com/blog/2018/07/04/graphql-abstract-syntax-tree-new-schema/). The advantage of parsing to an AST (on the client-side) is that it allows you to lint your queries and eases query handling e.g. with fragments (see: https://github.com/apollographql/graphql-tag#why-use-this)
如果您想将查询传递给 axios/fetch,您需要使用 graphql 打印机 fn (https://graphql.org/graphql-js/language/#printer):[=14= 将其解析回实际的查询字符串]
import gql from 'graphql-tag';
import { print } from 'graphql/language/printer';
const AST = gql`
{
user(id: 5) {
firstName
lastName
}
}
`
const query = print(AST);
我有一个简单的 axios 调用,我想调用它来获取一些 graphql 数据。
我还有一个 .gql
文件,我想将其导入以用作查询:
pages.gql
{
entries(section: [pages]) {
uri
slug
}
}
现在我想在我的其他文件中导入:
import query from './queries/pages.gql'
稍后进行 axios 调用:
const routes = await axios.post(
endpoint,
{ query },
{ headers: { Authorization: `Bearer ${token}` } }
)
.then(result => {
console.log('result: ', result)
})
.catch(error => {
console.log('error: ', error)
})
但是每当我这样做时,我都会得到这个错误:
missing ) after argument list 09:55:25
entries(section: [pages]) {
^^^^^^^
SyntaxError: missing ) after argument list
所以我不能只导入 gql 文件。
我已导入 graphql-tag
(import gql from 'graphql-tag'
),所以我可以这样做:
const QUERY = gql`
${query}
`
但首先我必须导入 gql 文件,对吗?
任何帮助将不胜感激... 干杯
---- 编辑 2019-08-28:----
好的,另一种情况:
我有一个非常有用的 axios 调用:
await axios
.post(
endpoint,
{ query: `query Page($slug: String!) {
entries(section: [pages], slug: $slug) {
slug
title
}
}`,
variables: { slug: params.slug }
},
{ headers: { Authorization: `Bearer ${env.GRAPHQL_TOKEN}` } }
).then(result => {
console.log('result: ', result)
})
.catch(error => {
console.log('error: ', error)
})
我还有一个 page.gql 文件,内容完全相同:
query Page($slug: String!) {
entries(section: [pages], slug: $slug) {
slug
title
}
}
我发现像这样导入 page.gql 文件
import page from '~/apollo/queries/page'
将导入这种结构的东西:
page: { 15:16:40
kind: 'Document',
definitions: [...],
loc: {
start: 0,
end: 181,
source: {
body: 'query Page($slug: String!) {\n ' +
'entries(section: [pages], slug: $slug) {\n ' +
'slug\n title\n }\n }\n}\n',
name: 'GraphQL request',
locationOffset: [Object]
}
},
Page: {...}
}
所以要在我的查询中使用 page.gql 我必须使用这个 page.loc.source.body
:
await axios
.post(
endpoint,
{ query: page.loc.source.body,
variables: { slug: params.slug }
},
{ headers: { Authorization: `Bearer ${env.GRAPHQL_TOKEN}` } }
).then(result => {
console.log('result: ', result)
})
.catch(error => {
console.log('error: ', error)
})
还有比这更好的方法吗?
graphql 标签模板文字(gql`..`
)将 graphql 查询解析为 AST(抽象语法树;进一步阅读:https://www.contentful.com/blog/2018/07/04/graphql-abstract-syntax-tree-new-schema/). The advantage of parsing to an AST (on the client-side) is that it allows you to lint your queries and eases query handling e.g. with fragments (see: https://github.com/apollographql/graphql-tag#why-use-this)
如果您想将查询传递给 axios/fetch,您需要使用 graphql 打印机 fn (https://graphql.org/graphql-js/language/#printer):[=14= 将其解析回实际的查询字符串]
import gql from 'graphql-tag';
import { print } from 'graphql/language/printer';
const AST = gql`
{
user(id: 5) {
firstName
lastName
}
}
`
const query = print(AST);