Passing array of strings to a GraphQL query, convers to integers when embedded
I'm trying to figure out a way to pass an array of strings and embed it into a query (using React, GraphQL). The problem is it accepts the parameter as an array of strings, but converts it to a string when I embed it.
Let's say I have this function that makes a call to get some data. (I hardcoded the argument for now but it will be a variable as soon as I figure this out).
// here I'm calling the function
const query = getData.generate(["123", "456"]);
return GraphqlClient.query(query)
.then((e) => {
return e.data.oneAppProviders;
}) .......
// and here is the query with the embedded parameter. (Backend expects an array of strings.)
export default {
generate(id) {
// console.log(id) // output: ["123", "456"]
return { query : gql`{
oneAppProviders(id: ${id}) {
id
firstName
}
}
}}
When I run it, I get this error:
GraphQLError {message: "Syntax Error: Expected Name, found Int "456""
I guess, when I embed it, it converts it to integers...
If my array is ["123"], I get the following error:
[GraphQL error]: Message: Expected type [String], found 123.
I hope the question is clear enough. Thanks in advance.
javascript reactjs graphql template-literals
add a comment |
I'm trying to figure out a way to pass an array of strings and embed it into a query (using React, GraphQL). The problem is it accepts the parameter as an array of strings, but converts it to a string when I embed it.
Let's say I have this function that makes a call to get some data. (I hardcoded the argument for now but it will be a variable as soon as I figure this out).
// here I'm calling the function
const query = getData.generate(["123", "456"]);
return GraphqlClient.query(query)
.then((e) => {
return e.data.oneAppProviders;
}) .......
// and here is the query with the embedded parameter. (Backend expects an array of strings.)
export default {
generate(id) {
// console.log(id) // output: ["123", "456"]
return { query : gql`{
oneAppProviders(id: ${id}) {
id
firstName
}
}
}}
When I run it, I get this error:
GraphQLError {message: "Syntax Error: Expected Name, found Int "456""
I guess, when I embed it, it converts it to integers...
If my array is ["123"], I get the following error:
[GraphQL error]: Message: Expected type [String], found 123.
I hope the question is clear enough. Thanks in advance.
javascript reactjs graphql template-literals
add a comment |
I'm trying to figure out a way to pass an array of strings and embed it into a query (using React, GraphQL). The problem is it accepts the parameter as an array of strings, but converts it to a string when I embed it.
Let's say I have this function that makes a call to get some data. (I hardcoded the argument for now but it will be a variable as soon as I figure this out).
// here I'm calling the function
const query = getData.generate(["123", "456"]);
return GraphqlClient.query(query)
.then((e) => {
return e.data.oneAppProviders;
}) .......
// and here is the query with the embedded parameter. (Backend expects an array of strings.)
export default {
generate(id) {
// console.log(id) // output: ["123", "456"]
return { query : gql`{
oneAppProviders(id: ${id}) {
id
firstName
}
}
}}
When I run it, I get this error:
GraphQLError {message: "Syntax Error: Expected Name, found Int "456""
I guess, when I embed it, it converts it to integers...
If my array is ["123"], I get the following error:
[GraphQL error]: Message: Expected type [String], found 123.
I hope the question is clear enough. Thanks in advance.
javascript reactjs graphql template-literals
I'm trying to figure out a way to pass an array of strings and embed it into a query (using React, GraphQL). The problem is it accepts the parameter as an array of strings, but converts it to a string when I embed it.
Let's say I have this function that makes a call to get some data. (I hardcoded the argument for now but it will be a variable as soon as I figure this out).
// here I'm calling the function
const query = getData.generate(["123", "456"]);
return GraphqlClient.query(query)
.then((e) => {
return e.data.oneAppProviders;
}) .......
// and here is the query with the embedded parameter. (Backend expects an array of strings.)
export default {
generate(id) {
// console.log(id) // output: ["123", "456"]
return { query : gql`{
oneAppProviders(id: ${id}) {
id
firstName
}
}
}}
When I run it, I get this error:
GraphQLError {message: "Syntax Error: Expected Name, found Int "456""
I guess, when I embed it, it converts it to integers...
If my array is ["123"], I get the following error:
[GraphQL error]: Message: Expected type [String], found 123.
I hope the question is clear enough. Thanks in advance.
javascript reactjs graphql template-literals
javascript reactjs graphql template-literals
asked Aug 16 '18 at 21:25
ElenaElena
61
61
add a comment |
add a comment |
2 Answers
2
active
oldest
votes
A simple solution to your problem is as follows:
let myarray = ["string1", "string2", "String3"];
let dataToSend = myarray.toString();
let gql`
{
passArray(data:${dataToSend}){
}
}`;
add a comment |
As a general rule you should avoid embedding data into query language strings like this. (What if the id variable is actually a string containing parentheses and curly braces? https://xkcd.com/327 is a more famous, if fictional, example of the potential problems you're facing.)
GraphQL supports top-level query parameters and you should use those here. If you make your query like
query Providers($ids: [ID!]!) {
oneAppProviders(id: $ids) {
id
firstName
}
}
most query libraries have a way to pass in additional parameters; so in your case that might look like
const query = gql`...`;
const variables = { ids: ids };
GraphqlClient.query(query, variables).then(...);
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f51885547%2fpassing-array-of-strings-to-a-graphql-query-convers-to-integers-when-embedded%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
A simple solution to your problem is as follows:
let myarray = ["string1", "string2", "String3"];
let dataToSend = myarray.toString();
let gql`
{
passArray(data:${dataToSend}){
}
}`;
add a comment |
A simple solution to your problem is as follows:
let myarray = ["string1", "string2", "String3"];
let dataToSend = myarray.toString();
let gql`
{
passArray(data:${dataToSend}){
}
}`;
add a comment |
A simple solution to your problem is as follows:
let myarray = ["string1", "string2", "String3"];
let dataToSend = myarray.toString();
let gql`
{
passArray(data:${dataToSend}){
}
}`;
A simple solution to your problem is as follows:
let myarray = ["string1", "string2", "String3"];
let dataToSend = myarray.toString();
let gql`
{
passArray(data:${dataToSend}){
}
}`;
edited Nov 20 '18 at 8:32
Unheilig
12k165387
12k165387
answered Nov 20 '18 at 8:14
Sohail ArifSohail Arif
5114
5114
add a comment |
add a comment |
As a general rule you should avoid embedding data into query language strings like this. (What if the id variable is actually a string containing parentheses and curly braces? https://xkcd.com/327 is a more famous, if fictional, example of the potential problems you're facing.)
GraphQL supports top-level query parameters and you should use those here. If you make your query like
query Providers($ids: [ID!]!) {
oneAppProviders(id: $ids) {
id
firstName
}
}
most query libraries have a way to pass in additional parameters; so in your case that might look like
const query = gql`...`;
const variables = { ids: ids };
GraphqlClient.query(query, variables).then(...);
add a comment |
As a general rule you should avoid embedding data into query language strings like this. (What if the id variable is actually a string containing parentheses and curly braces? https://xkcd.com/327 is a more famous, if fictional, example of the potential problems you're facing.)
GraphQL supports top-level query parameters and you should use those here. If you make your query like
query Providers($ids: [ID!]!) {
oneAppProviders(id: $ids) {
id
firstName
}
}
most query libraries have a way to pass in additional parameters; so in your case that might look like
const query = gql`...`;
const variables = { ids: ids };
GraphqlClient.query(query, variables).then(...);
add a comment |
As a general rule you should avoid embedding data into query language strings like this. (What if the id variable is actually a string containing parentheses and curly braces? https://xkcd.com/327 is a more famous, if fictional, example of the potential problems you're facing.)
GraphQL supports top-level query parameters and you should use those here. If you make your query like
query Providers($ids: [ID!]!) {
oneAppProviders(id: $ids) {
id
firstName
}
}
most query libraries have a way to pass in additional parameters; so in your case that might look like
const query = gql`...`;
const variables = { ids: ids };
GraphqlClient.query(query, variables).then(...);
As a general rule you should avoid embedding data into query language strings like this. (What if the id variable is actually a string containing parentheses and curly braces? https://xkcd.com/327 is a more famous, if fictional, example of the potential problems you're facing.)
GraphQL supports top-level query parameters and you should use those here. If you make your query like
query Providers($ids: [ID!]!) {
oneAppProviders(id: $ids) {
id
firstName
}
}
most query libraries have a way to pass in additional parameters; so in your case that might look like
const query = gql`...`;
const variables = { ids: ids };
GraphqlClient.query(query, variables).then(...);
answered Aug 16 '18 at 23:41
David MazeDavid Maze
14.2k31327
14.2k31327
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f51885547%2fpassing-array-of-strings-to-a-graphql-query-convers-to-integers-when-embedded%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown