Meteor with GraphQL

Integrate Meteor with GraphQL for a flexible and modern API approach.

  1. Install Apollo and GraphQL Packages:
meteor add apollo meteor npm install graphql apollo-server
  1. Set Up Apollo Server:
import { ApolloServer, gql } from 'apollo-server'; const typeDefs = gql` type Query { items: [Item] } type Item { _id: ID name: String quantity: Int } `; const resolvers = { Query: { items: () => Items.find().fetch() } }; const server = new ApolloServer({ typeDefs, resolvers }); server.listen().then(({ url }) => { console.log(`🚀 Server ready at ${url}`); });
  1. Querying GraphQL from Meteor:Use Apollo Client or a similar library to query GraphQL endpoints.
import { ApolloClient, InMemoryCache, gql } from '@apollo/client'; const client = new ApolloClient({ uri: 'http://localhost:4000/', cache: new InMemoryCache() }); client.query({ query: gql` query { items { _id name quantity } } ` }).then(response => console.log(response.data));

Comments

Leave a Reply

Your email address will not be published. Required fields are marked *