node.js – How to connect GraphQL/Node/React

How do I connect my GraphQL to Node? Can anyone advise me a documentation?

I have a schema:

const { makeExecutableSchema } = require('@graphql-tools/schema');

const typeDefs = `#graphql

    type Query {
        tasks: [Task!]!
    } 

    type Mutation{
        addTask( input: taskInput): Task
    }

    input taskInput{
        name: String!,
        done: Boolean!,
        _id: ID!
    }

    type Task {
        name: String!
        done: Boolean!
        _id: ID!
    }
`
const schema = makeExecutableSchema({ typeDefs });
module.exports = {schema}

and I have resolver:

const {client} = require('./controllers/tasks')


const resolvers = {
    Query:{
      
        tasks: async (parent,args,context,info) =>{
          return await client.search({
                index: 'tasks',
                body:{
                  query:{
                    match_all: {}
                  }
                }
              })
        }
    },

    Mutation: {
      addTask: async (parent, { input }) => {
        const newTask = {
          name: input.name,
          done: false,
          _id: ''
        };
        const createdTask = await client.create({
          index: 'tasks',
          body: newTask
        });
        newTask._id = createdTask._id;
        return newTask
      }
    }
}
module.exports = {resolvers}

How do I see the data inside GraphQL? I can see the schema I created but I don’t know how to take data from my schema.

I’m not even sure if i have to use @apollo/server or apollo-server-express or express-graphql.

const express = require('express')
const app = express()
const tasks = require('./routes/tasks')
const { ApolloServer } = require('@apollo/server');
const {startStandaloneServer} = require('@apollo/server/standalone')
const { schema } =  require('./schema.js');
const { resolvers } =  require('./resolvers.js');

app.use(express.json())
app.use(express.urlencoded({extended:false}))

//app.get('/api',tasks)
//app.post('/api',tasks)
//app.put('/api/done/:id',tasks)
//app.delete('/api/delete/:id',tasks)
//app.put('/api/rename/:id',tasks)


const server = new ApolloServer({ schema, resolvers, path: '/graphql' });
const startServer = async () => {  
    await startStandaloneServer(server,{
        listen:{port:5555},
    })
    console.log(`Server ready at: localhost:5555`);
    console.log('Query at: https://studio.apollographql.com/dev');
}
startServer() 

app.listen(5551, () =>{
    console.log('port: 5551');
})  

Read more here: Source link