How to use variables in a GraphQL query?











up vote
1
down vote

favorite












So I have to make two queries. First will return an array with multiple objects and I want to get the ID of the first object to use it in my second query.



The problem is that I can't use b_id: props.getBusiness.business[0]._id



Any idea how can I work with this?



const GET_USER_BUSINESS = gql`
query getUserBusiness {
getUserBusiness {
_id
}
}
`;

const GET_BUSINESS_JOBS = gql`
query getBusinessJobs($b_id: ID!) {
getBusinessJobs(b_id: $b_id ) {
_id
name
}
}
`;

export default compose(
withApollo,
graphql(GET_USER_BUSINESS,
{
name: "business"
}),
graphql(GET_BUSINESS_JOBS,
{
name: "jobs",
options: (props) => (
{
variables:
{
b_id: props.getUserBusiness.business[0].b_id
}
}
)
})
)(ProposalContainer);









share|improve this question






















  • Do you control the server end of this too?
    – David Maze
    Nov 7 at 18:52










  • @DavidMaze no it is just a query
    – Markus Hayner
    Nov 7 at 19:04










  • getUserBusiness sounds like it would return a single object and not an array... does it?
    – Daniel Rearden
    Nov 8 at 5:32















up vote
1
down vote

favorite












So I have to make two queries. First will return an array with multiple objects and I want to get the ID of the first object to use it in my second query.



The problem is that I can't use b_id: props.getBusiness.business[0]._id



Any idea how can I work with this?



const GET_USER_BUSINESS = gql`
query getUserBusiness {
getUserBusiness {
_id
}
}
`;

const GET_BUSINESS_JOBS = gql`
query getBusinessJobs($b_id: ID!) {
getBusinessJobs(b_id: $b_id ) {
_id
name
}
}
`;

export default compose(
withApollo,
graphql(GET_USER_BUSINESS,
{
name: "business"
}),
graphql(GET_BUSINESS_JOBS,
{
name: "jobs",
options: (props) => (
{
variables:
{
b_id: props.getUserBusiness.business[0].b_id
}
}
)
})
)(ProposalContainer);









share|improve this question






















  • Do you control the server end of this too?
    – David Maze
    Nov 7 at 18:52










  • @DavidMaze no it is just a query
    – Markus Hayner
    Nov 7 at 19:04










  • getUserBusiness sounds like it would return a single object and not an array... does it?
    – Daniel Rearden
    Nov 8 at 5:32













up vote
1
down vote

favorite









up vote
1
down vote

favorite











So I have to make two queries. First will return an array with multiple objects and I want to get the ID of the first object to use it in my second query.



The problem is that I can't use b_id: props.getBusiness.business[0]._id



Any idea how can I work with this?



const GET_USER_BUSINESS = gql`
query getUserBusiness {
getUserBusiness {
_id
}
}
`;

const GET_BUSINESS_JOBS = gql`
query getBusinessJobs($b_id: ID!) {
getBusinessJobs(b_id: $b_id ) {
_id
name
}
}
`;

export default compose(
withApollo,
graphql(GET_USER_BUSINESS,
{
name: "business"
}),
graphql(GET_BUSINESS_JOBS,
{
name: "jobs",
options: (props) => (
{
variables:
{
b_id: props.getUserBusiness.business[0].b_id
}
}
)
})
)(ProposalContainer);









share|improve this question













So I have to make two queries. First will return an array with multiple objects and I want to get the ID of the first object to use it in my second query.



The problem is that I can't use b_id: props.getBusiness.business[0]._id



Any idea how can I work with this?



const GET_USER_BUSINESS = gql`
query getUserBusiness {
getUserBusiness {
_id
}
}
`;

const GET_BUSINESS_JOBS = gql`
query getBusinessJobs($b_id: ID!) {
getBusinessJobs(b_id: $b_id ) {
_id
name
}
}
`;

export default compose(
withApollo,
graphql(GET_USER_BUSINESS,
{
name: "business"
}),
graphql(GET_BUSINESS_JOBS,
{
name: "jobs",
options: (props) => (
{
variables:
{
b_id: props.getUserBusiness.business[0].b_id
}
}
)
})
)(ProposalContainer);






reactjs react-native graphql






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 7 at 17:28









Markus Hayner

549211




549211












  • Do you control the server end of this too?
    – David Maze
    Nov 7 at 18:52










  • @DavidMaze no it is just a query
    – Markus Hayner
    Nov 7 at 19:04










  • getUserBusiness sounds like it would return a single object and not an array... does it?
    – Daniel Rearden
    Nov 8 at 5:32


















  • Do you control the server end of this too?
    – David Maze
    Nov 7 at 18:52










  • @DavidMaze no it is just a query
    – Markus Hayner
    Nov 7 at 19:04










  • getUserBusiness sounds like it would return a single object and not an array... does it?
    – Daniel Rearden
    Nov 8 at 5:32
















Do you control the server end of this too?
– David Maze
Nov 7 at 18:52




Do you control the server end of this too?
– David Maze
Nov 7 at 18:52












@DavidMaze no it is just a query
– Markus Hayner
Nov 7 at 19:04




@DavidMaze no it is just a query
– Markus Hayner
Nov 7 at 19:04












getUserBusiness sounds like it would return a single object and not an array... does it?
– Daniel Rearden
Nov 8 at 5:32




getUserBusiness sounds like it would return a single object and not an array... does it?
– Daniel Rearden
Nov 8 at 5:32












1 Answer
1






active

oldest

votes

















up vote
0
down vote



accepted










Couple of things. One, by default, the graphql HOC passes a prop called data down to the wrapped component with the query data. However, because you specify a name in the HOC's config, the prop will actually be called whatever name you pass it. In other words, you can access the _id this way:



props.business.getUserBusiness[0]._id


...if getUserBusiness returns an array. Otherwise:



props.business.getUserBusiness._id


Secondly, props.business will be undefined until the query completes. We probably don't want to send the GET_BUSINESS_JOBS query until we have an _id to work with. So we want to pass a skip function to the HOC's config:



export default compose(
withApollo,
graphql(GET_USER_BUSINESS,
{
name: "business"
}),
graphql(GET_BUSINESS_JOBS,
{
name: "jobs",
skip: (props) => !props.business || !props.business.getUserBusiness[0]
options: (props) => (
{
variables:
{
b_id: props.business.getUserBusiness[0]._id
}
}
)
})
)(ProposalContainer)





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',
    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%2f53194708%2fhow-to-use-variables-in-a-graphql-query%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    1 Answer
    1






    active

    oldest

    votes








    1 Answer
    1






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes








    up vote
    0
    down vote



    accepted










    Couple of things. One, by default, the graphql HOC passes a prop called data down to the wrapped component with the query data. However, because you specify a name in the HOC's config, the prop will actually be called whatever name you pass it. In other words, you can access the _id this way:



    props.business.getUserBusiness[0]._id


    ...if getUserBusiness returns an array. Otherwise:



    props.business.getUserBusiness._id


    Secondly, props.business will be undefined until the query completes. We probably don't want to send the GET_BUSINESS_JOBS query until we have an _id to work with. So we want to pass a skip function to the HOC's config:



    export default compose(
    withApollo,
    graphql(GET_USER_BUSINESS,
    {
    name: "business"
    }),
    graphql(GET_BUSINESS_JOBS,
    {
    name: "jobs",
    skip: (props) => !props.business || !props.business.getUserBusiness[0]
    options: (props) => (
    {
    variables:
    {
    b_id: props.business.getUserBusiness[0]._id
    }
    }
    )
    })
    )(ProposalContainer)





    share|improve this answer

























      up vote
      0
      down vote



      accepted










      Couple of things. One, by default, the graphql HOC passes a prop called data down to the wrapped component with the query data. However, because you specify a name in the HOC's config, the prop will actually be called whatever name you pass it. In other words, you can access the _id this way:



      props.business.getUserBusiness[0]._id


      ...if getUserBusiness returns an array. Otherwise:



      props.business.getUserBusiness._id


      Secondly, props.business will be undefined until the query completes. We probably don't want to send the GET_BUSINESS_JOBS query until we have an _id to work with. So we want to pass a skip function to the HOC's config:



      export default compose(
      withApollo,
      graphql(GET_USER_BUSINESS,
      {
      name: "business"
      }),
      graphql(GET_BUSINESS_JOBS,
      {
      name: "jobs",
      skip: (props) => !props.business || !props.business.getUserBusiness[0]
      options: (props) => (
      {
      variables:
      {
      b_id: props.business.getUserBusiness[0]._id
      }
      }
      )
      })
      )(ProposalContainer)





      share|improve this answer























        up vote
        0
        down vote



        accepted







        up vote
        0
        down vote



        accepted






        Couple of things. One, by default, the graphql HOC passes a prop called data down to the wrapped component with the query data. However, because you specify a name in the HOC's config, the prop will actually be called whatever name you pass it. In other words, you can access the _id this way:



        props.business.getUserBusiness[0]._id


        ...if getUserBusiness returns an array. Otherwise:



        props.business.getUserBusiness._id


        Secondly, props.business will be undefined until the query completes. We probably don't want to send the GET_BUSINESS_JOBS query until we have an _id to work with. So we want to pass a skip function to the HOC's config:



        export default compose(
        withApollo,
        graphql(GET_USER_BUSINESS,
        {
        name: "business"
        }),
        graphql(GET_BUSINESS_JOBS,
        {
        name: "jobs",
        skip: (props) => !props.business || !props.business.getUserBusiness[0]
        options: (props) => (
        {
        variables:
        {
        b_id: props.business.getUserBusiness[0]._id
        }
        }
        )
        })
        )(ProposalContainer)





        share|improve this answer












        Couple of things. One, by default, the graphql HOC passes a prop called data down to the wrapped component with the query data. However, because you specify a name in the HOC's config, the prop will actually be called whatever name you pass it. In other words, you can access the _id this way:



        props.business.getUserBusiness[0]._id


        ...if getUserBusiness returns an array. Otherwise:



        props.business.getUserBusiness._id


        Secondly, props.business will be undefined until the query completes. We probably don't want to send the GET_BUSINESS_JOBS query until we have an _id to work with. So we want to pass a skip function to the HOC's config:



        export default compose(
        withApollo,
        graphql(GET_USER_BUSINESS,
        {
        name: "business"
        }),
        graphql(GET_BUSINESS_JOBS,
        {
        name: "jobs",
        skip: (props) => !props.business || !props.business.getUserBusiness[0]
        options: (props) => (
        {
        variables:
        {
        b_id: props.business.getUserBusiness[0]._id
        }
        }
        )
        })
        )(ProposalContainer)






        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Nov 8 at 5:35









        Daniel Rearden

        11.5k1935




        11.5k1935






























             

            draft saved


            draft discarded



















































             


            draft saved


            draft discarded














            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53194708%2fhow-to-use-variables-in-a-graphql-query%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







            這個網誌中的熱門文章

            Tangent Lines Diagram Along Smooth Curve

            Yusuf al-Mu'taman ibn Hud

            Zucchini