menu

Apollo

A community of developers, designers and others who love Apollo and GraphQL. 🚀

Channels
# All channels
view-forward
# General
view-forward
# Apollo Angular
view-forward
# Announcements
view-forward
# Apollo Android
view-forward
# Apollo Client
view-forward
# Apollo iOS
view-forward
# Apollo Link
view-forward
# Apollo Link Rest
view-forward
# Local State
view-forward
# Apollo Studio
view-forward
# Apollo Server
view-forward
# Apollo Tooling
view-forward
# Contributing
view-forward
# Docs
view-forward
# Events
view-forward
# GraphQL Tools
view-forward
# Jobs
view-forward
# Random
view-forward
# React Apollo
view-forward
# Showcase
view-forward
# Subscriptions
view-forward
# Testing
view-forward
# Vue Apollo
view-forward
Team

Newbie looking for advise on how to create an Apollo GraphQL API

March 24, 2021 at 11:15pm

Newbie looking for advise on how to create an Apollo GraphQL API

March 24, 2021 at 11:15pm
Hi everyone, I have just joined a new company and I am given a task as an assessment to use GraphQL along with React and till now I have only used REST I have no clue how GraphQL works and the books and tutorials I find seem to be so confusing since I am used to the REST way of creating multiple end points. Can you please shed light on how I can solve this problem, I must consume this Apollo GraphQL in React:
  1. You should develop an Apollo GraphQL API
  2. Your GraphQL API should wrap the Star Wars API (https://swapi.dev/)
  3. Your GraphQL API should have a Query type that resolves all People (https://swapi.dev/api/people/), but only the Person's details (name, height, mass, gender, homeworld).
  4. The People Query should cater for pagination, you will notice the next property in the response. When given a page number, the respective People page should be returned (i.e. https://swapi.dev/api/people/?page=2)
  5. Your GraphQL API should have a Query type that resolves (searches for) a particular Person (People) given their name (i.e. https://swapi.dev/api/people/?search=Anakin Skywalker)
Your help in this will be highly appreciated, thank you in advance

March 25, 2021 at 9:56am
First, you must setup own GraphQL server (reference: https://www.apollographql.com/docs/apollo-server/getting-started/). i usually use apollo-server-express then you create your own schemas and resolvers based on Stars Wars API (persons, personById, etc). you cant wrap Star Wars API into your GraphQL server, need to create from scratch
I hope this helps you.
Edited
like-fill
1
  • reply
  • like
Hi Agit thank you very much for your response, I have already created the server I used apollo-server
I am struggling with the resolvers since this is a new concept to me, I think by saying wrap the Star Wars API I mean fetch data from it under the resolver. I am not sure how to go about this but by reading online I only find examples of mocked data not api data and in my case I need to fetch data from the Star Wars API
  • reply
  • like
I see. So actually first thing you need to do is define your schema which list field name want to you return, then you define resolver (logic) to get data from database.
You need connect your server apollo with ORM (Sequelize, Objection, TypeORM, etc) and database to get data.
Oh ya, also you need setup connection in index.js or app.js with ORM that i list above.
Here the simple example:
schema.js
Query {
peoples: [People]
}
type People {
id: ID
name: String
hair: String
mass: Double
address: String
}
resolver.js
// IMPORT MODEL
const { User } = require('../models');
const resolvers = {
Query: {
async peoples(_, args, context) {
// DO LOGIC HERE
return User.query();
},
}
};
if you wish, I can make a complete simple server wiht database connection
like-fill
1
  • reply
  • like
Wait I solved the problem following your logic, Thank you very much you sure saved me. Everything is up and running now
  • reply
  • like

March 26, 2021 at 2:23am
okay nice, glad to help you
  • reply
  • like