Does apollo-client work on node.js?

前端 未结 7 1553
醉酒成梦
醉酒成梦 2020-12-16 09:34

I need a graphql client lib to run on node.js for some testing and some data mashup - not in a production capacity. I\'m using apollo everywhere else (

7条回答
  •  北荒
    北荒 (楼主)
    2020-12-16 10:14

    Apollo Client should work just fine on Node. You only have to install cross-fetch because it assumes fetch exists.

    Here is a complete TypeScript implementation of Apollo Client working on Node.js.

    import ApolloClient, { gql } from "apollo-boost";
    
    import { InsertJob } from "./graphql-types";
    import 'cross-fetch/polyfill';
    
    const client = new ApolloClient({
      uri: "http://localhost:3000/graphql"
    });
    
    
    client.mutate({
      mutation: gql`mutation insertJob($companyName: String!) {
          addCompany(input: { displayName: $companyName } ) {
              id
          }
      }`,
      variables: {
        companyName: "aaa"
      }
    })
      .then(result => console.log(result));
    

提交回复
热议问题