Passing array of strings to a GraphQL query, convers to integers when embedded












1















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.










share|improve this question



























    1















    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.










    share|improve this question

























      1












      1








      1








      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.










      share|improve this question














      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






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Aug 16 '18 at 21:25









      ElenaElena

      61




      61
























          2 Answers
          2






          active

          oldest

          votes


















          5














          A simple solution to your problem is as follows:



          let myarray = ["string1", "string2", "String3"];
          let dataToSend = myarray.toString();
          let gql`
          {
          passArray(data:${dataToSend}){
          }
          }`;





          share|improve this answer

































            1














            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(...);





            share|improve this answer























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


              }
              });














              draft saved

              draft discarded


















              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









              5














              A simple solution to your problem is as follows:



              let myarray = ["string1", "string2", "String3"];
              let dataToSend = myarray.toString();
              let gql`
              {
              passArray(data:${dataToSend}){
              }
              }`;





              share|improve this answer






























                5














                A simple solution to your problem is as follows:



                let myarray = ["string1", "string2", "String3"];
                let dataToSend = myarray.toString();
                let gql`
                {
                passArray(data:${dataToSend}){
                }
                }`;





                share|improve this answer




























                  5












                  5








                  5







                  A simple solution to your problem is as follows:



                  let myarray = ["string1", "string2", "String3"];
                  let dataToSend = myarray.toString();
                  let gql`
                  {
                  passArray(data:${dataToSend}){
                  }
                  }`;





                  share|improve this answer















                  A simple solution to your problem is as follows:



                  let myarray = ["string1", "string2", "String3"];
                  let dataToSend = myarray.toString();
                  let gql`
                  {
                  passArray(data:${dataToSend}){
                  }
                  }`;






                  share|improve this answer














                  share|improve this answer



                  share|improve this answer








                  edited Nov 20 '18 at 8:32









                  Unheilig

                  12k165387




                  12k165387










                  answered Nov 20 '18 at 8:14









                  Sohail ArifSohail Arif

                  5114




                  5114

























                      1














                      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(...);





                      share|improve this answer




























                        1














                        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(...);





                        share|improve this answer


























                          1












                          1








                          1







                          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(...);





                          share|improve this answer













                          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(...);






                          share|improve this answer












                          share|improve this answer



                          share|improve this answer










                          answered Aug 16 '18 at 23:41









                          David MazeDavid Maze

                          14.2k31327




                          14.2k31327






























                              draft saved

                              draft discarded




















































                              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.




                              draft saved


                              draft discarded














                              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





















































                              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







                              這個網誌中的熱門文章

                              Academy of Television Arts & Sciences

                              L'Équipe

                              1995 France bombings