Apollo-link-rest: Provide example with mutation

Created on 18 Mar 2018  ·  2Comments  ·  Source: apollographql/apollo-link-rest

Can you please provide example of POST/PUT/DELETE mutations with apollo-link-rest?

I have found small example here: https://github.com/sabativi/apollo-link/blob/apollo-link-rest/docs/source/links/rest.md
But no info on bodyKey or bodyBuilder.

I am asking mainly because in my code, i am trying to make POST request with this:

const createAccountMutation = gql`
    mutation createAccount($email: String!, $password: String!, $name: String) {
        createAccount(email: $email, password: $password, name: $name)
            @rest(type: "User", path: "/register", method: "POST") {
            response
        }
    }
`;
// And then call it inside component with:
    this.props.createAccount({
        variables: {
            email: values.email, 
            name: values.name,
            password: values.password,
        }
    })

But app responds with ApolloError: Error: Network error: [GraphQL mutation using a REST call without a body]. No `input` was detected. Pass bodyKey, or bodyBuilder to the @rest() directive to resolve this.

Should all the variables be wrapped inside input property? If so, why?

Thanks,
Jiří

question❔

Most helpful comment

Hi @jdolinski1! We're still working on the documentation, but here are some quick answers to your questions:

  • Why input?: There is a convention for GraphQL of naming your mutation's arguments inside an input-type. https://graphql.org/graphql-js/mutations-and-input-types/ (and often this is named input)
  • bodyKey lets you override the key we look at to find the fields you want to send as the body of the REST-POST request.
  • bodyBuilder is a function that lets you build a completely custom body for the request using your own rules and code.

Examples can be found here: https://github.com/apollographql/apollo-link-rest/blob/c9d81ae308e5f61b5ae992061de7abc6cb2f78e0/src/__tests__/restLink.ts#L1743-L1905

All 2 comments

Hi @jdolinski1! We're still working on the documentation, but here are some quick answers to your questions:

  • Why input?: There is a convention for GraphQL of naming your mutation's arguments inside an input-type. https://graphql.org/graphql-js/mutations-and-input-types/ (and often this is named input)
  • bodyKey lets you override the key we look at to find the fields you want to send as the body of the REST-POST request.
  • bodyBuilder is a function that lets you build a completely custom body for the request using your own rules and code.

Examples can be found here: https://github.com/apollographql/apollo-link-rest/blob/c9d81ae308e5f61b5ae992061de7abc6cb2f78e0/src/__tests__/restLink.ts#L1743-L1905

I have a section on this in the WIP documentation so I'm going to close this ticket for now.

Was this page helpful?
0 / 5 - 0 ratings