Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
319 views
in Technique[技术] by (71.8m points)

javascript - 如何从node.js函数将变量传递到graphql-tag?(How to pass variables into graphql-tag from node.js function?)

I have the following function:(我有以下功能:)

import ApolloClient from 'apollo-boost' import gql from 'graphql-tag' import fetch from 'node-fetch' global.fetch = fetch const client = new ApolloClient({ uri: 'myUri' }) const getPostsByCategory = async category => { const res = await client.query({ query: gql` query articlesByCategory($id: String!) { postsByCategory(id: $id) { id } } ` }) console.log('res', res) } I want to invoke the function as:(我想将函数调用为:) await getPostsByCategory('news') however I just can't understand how I pass the category variable into the query.(但是我只是不明白如何将类别变量传递给查询。) I want to use qraphql-tag in my queries and not pass a simple tagged literal as the query.(我想在查询中使用qraphql-tagqraphql-tag要将简单的带标记文字作为查询传递。)   ask by hitchhiker translate from so

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

You can use the variables key in your client.query function params as the following(您可以在client.query函数参数中使用variables键,如下所示)

const getPostsByCategory = async category => { const res = await client.query({ query: gql` query articlesByCategory($id: String!) { postsByCategory(id: $id) { id } } `, variables: { id: category, }, }); console.log('res', res); };

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...