'Apollo GraphQl Backend using Fetch API mutation”翻译成中文为“使用Fetch API进行Apollo GraphQl后端的mutation”,其解决方法如下:
npm install apollo-client graphql-tag node-fetch
import { ApolloClient } from 'apollo-client'; import { createHttpLink } from 'apollo-link-http'; import { InMemoryCache } from 'apollo-cache-inmemory';
const httpLink = createHttpLink({ uri: 'https://api.graph.cool/simple/v1/mutationEndpoint', fetch: fetch })
const client = new ApolloClient({ link: httpLink, cache: new InMemoryCache() })
import gql from 'graphql-tag';
const MUTATION_NAME = gql mutation MutationName($inputValue: Type!) { mutationResolver(inputValue: $inputValue) { id name } }
;
client.mutate({ mutation: MUTATION_NAME, variables: { inputValue: 'Value' } }).then(response => { console.log('Mutation response: ', response); }).catch(error => { console.error('Mutation error: ', error); });
以上就是使用Fetch API进行Apollo GraphQl后端的mutation的解决方法了。