Skip to content

Instantly share code, notes, and snippets.

View unicodeveloper's full-sized avatar
🔥
Developing Platforms

Prosper Otemuyiwa unicodeveloper

🔥
Developing Platforms
View GitHub Profile
// Example of a default GraphQL response
const response = {
data: {
getUser: {
__typename: 'User',
uid: '5a6efb94b0e8c36f99fba013',
email: 'john.doe@yahoo.com',
},
},
}
import gql from 'graphql-tag'
// Apollo Client instance
import client from './apollo'
const file = new Blob(['Foo.'], { type: 'text/plain' })
// Optional, defaults to `blob`
file.name = 'bar.txt'
@unicodeveloper
unicodeveloper / multiple-file-upload.js
Last active July 10, 2018 17:06
Multiple file upload
import gql from 'graphql-tag'
import { Mutation } from 'react-apollo'
export const UPLOAD_MULTIPLE_FILES = gql`
mutation uploadMultipleFiles($files: [Upload!]!) {
uploadMultipleFiles(files: $files) {
filename
}
}
@unicodeveloper
unicodeveloper / single-file-upload.js
Last active November 30, 2019 10:59
Single file upload
import gql from 'graphql-tag'
import { Mutation } from 'react-apollo'
export const UPLOAD_FILE = gql`
mutation uploadFile($file: Upload!) {
uploadFile(file: $file) {
filename
}
}
`;
@unicodeveloper
unicodeveloper / resolver-implementation.js
Last active November 29, 2019 17:32
Resolver Implementation
const resolvers = {
Query: {
files: () => {
// Return the record of files uploaded from your DB or API or filesystem.
}
},
Mutation: {
async singleUpload(parent, { file }) {
const { stream, filename, mimetype, encoding } = await file;
const { ApolloServer, gql } = require('apollo-server');
const typeDefs = gql`
type File {
filename: String!
mimetype: String!
encoding: String!
}
type Query {
type Dog @cacheControl(maxAge: 120) {
id: String!
breed: String!
displayImage: String @cacheControl(maxAge: 240)
images: [Image]
subbreeds: [String]
}
@unicodeveloper
unicodeveloper / configure-engine-with-server.js
Created June 27, 2018 22:28
Configure Apollo Engine in Apollo Server 2.0
const { ApolloServer } = require("apollo-server");
const server = new ApolloSever({
typeDefs,
resolvers,
engine: {
apiKey: "YOUR API KEY HERE"
}
});
@unicodeveloper
unicodeveloper / configure-cache.js
Last active June 28, 2018 19:47
configure cache backend on Apollo Server
const { MemcachedCache } = require('apollo-server-memcached');
const { ApolloServe } = require('apollo-server');
const server = new ApolloServer({
...
persistedQueries: {
cache: new MemcachedCache(
['memcached-server-1', 'memcached-server-2', 'memcached-server-3'],
{ retries: 10, retry: 10000 }, // Options
),
import { createPersistedQueryLink } from "apollo-link-persisted-queries";
import { createHttpLink } from "apollo-link-http";
import { InMemoryCache } from "apollo-cache-inmemory";
import ApolloClient from "apollo-client";
const link = createPersistedQueryLink().concat(createHttpLink({ uri: "/graphql" }));
const client = new ApolloClient({
cache: new InMemoryCache(),
link: link,