Skip to content

pierrot-le-foo/apollo-schema-link

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

13 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Apollo link to use a in-memory graphql server.

Install

npm i pierrot-le-foo/apollo-schema-link

Get started

Simple example

// Our dependencies
import SchemaLink from "apollo-schema-link";
import { gql, ApolloClient, InMemoryCache } from "@apollo/client";
import { buildSchema } from "graphql";

const schema = buildSchema(`
  type User {
    id: ID!
    email: String!
  }

  type Query {
    getUsers: [User!]!
  }
`);
const rootValue = {
  async getUsers() {
    const response = await fetch("/api/users");
    return await response.json();
  },
};
const apolloClient = new ApolloClient({
  link: new SchemaLink({
    schema,
    rootValue,
  }),
  cache: new InMemoryCache(),
});
await apolloClient.query({
  query: gql`
    query {
      getUsers
    }
  `,
});

Simple example with React

// Our dependencies
import { useQuery } from "@apollo/client/react";

function Users() {
  const {
    data: { getUsers: users = [] } = {},
    error,
    loading,
  } = useQuery(operations.getUsers);

  if (error) {
    return <h4>Error {error.message}</h4>;
  }

  if (loading) {
    return <div>Loading</div>;
  }

  return (
    <table>
      {users.map((user) => (
        <tr key={user.id}>
          <td>{user.email}</td>
        </tr>
      ))}
    </table>
  );
}

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages