I
I
Igor Myasnikov2019-08-27 14:19:35
Node.js
Igor Myasnikov, 2019-08-27 14:19:35

How to connect graphQL with front?

Good afternoon!
There is a simple server on Node JS and a Database on Mongo Atlas, GraphQL is used for queries.
How can I link a ready front (html, css, native__js) to the server?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
M
Michael, 2019-08-27
@pilium

Igor Myasnikov , so, well, the
query itself is { operationName, query, variables }
For multipart { operations, map }

const query = `
  query($id: Int!) {
    user(id: $id) {
      name
    }
  }
`;

fetch('https://gql-placeholder.herokuapp.com/graphql', {
  method: 'POST',
  headers: {
    'content-type': 'application/json',
  },
  // credentials: 'include',
  body: JSON.stringify({
    operationName: null,
    query,
    variables: {
      id: 1,
    },
  }),
})
  .then((r) => r.json())
  .then(console.log);

You can run it in the console Files are sent a little differently, you need
to change the request itself (via FormData) https:
//github.com/jaydenseric/graphql-multipart-r ... ://www.npmjs.com/package/dahlia-graphql-client https://www.npmjs.com/package/gql-api-client

V
Vitaly, 2019-08-27
@vshvydky

Use Apollo it's convenient

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question