Skip to content

Instantly share code, notes, and snippets.

@kristianmandrup
Last active October 23, 2019 19:41
Show Gist options
  • Select an option

  • Save kristianmandrup/1f7e4a550e6c32b99881aa1b78a4a440 to your computer and use it in GitHub Desktop.

Select an option

Save kristianmandrup/1f7e4a550e6c32b99881aa1b78a4a440 to your computer and use it in GitHub Desktop.

Revisions

  1. kristianmandrup revised this gist Nov 16, 2018. 1 changed file with 16 additions and 23 deletions.
    39 changes: 16 additions & 23 deletions mockMang.js
    Original file line number Diff line number Diff line change
    @@ -2,7 +2,6 @@ import { makeExecutableSchema, addMockFunctionsToSchema } from "graphql-tools";
    import { graphql } from "graphql";
    import { ApolloClient } from "apollo-client";
    import { from } from "apollo-link";
    import gql from "graphql-tag";
    import { withClientState } from "apollo-link-state";
    import { InMemoryCache } from "apollo-cache-memory";
    import GoalsSchema from "../api/goals/Goal.graphql";
    @@ -39,19 +38,13 @@ const revisedRandId = () =>
    .substr(2, 10);

    // mock order
    const order = {

    }
    const order = {};
    // mock user
    const user = {

    }

    const product = {
    const user = {};

    }
    const product = {};

    const allProducts = [product]
    const allProducts = [product];

    const mocks = {
    ID: () => revisedRandId(),
    @@ -64,24 +57,24 @@ const mocks = {
    User: () => ({
    ...user
    })
    }
    };

    addMockFunctionsToSchema({schema, mocks})
    addMockFunctionsToSchema({ schema, mocks });

    const mockMang = await (query, args = {}) => {
    const mockMang = async (query, args = {}) => {
    try {
    const res = await graphql(schema, query.loc.source.body, null, null, args)
    res.data.loading = false
    return res.data
    const res = await graphql(schema, query.loc.source.body, null, null, args);
    res.data.loading = false;
    return res.data;
    } catch (err) {
    return console.log(err.message)
    return console.log(err.message);
    }
    }
    };

    export { client }
    export { client };

    export const mockFunc = () => null
    export const mockFunc = () => null;

    export const mockAsyncFunc = async () => true
    export const mockAsyncFunc = async () => true;

    export default mockMang
    export default mockMang;
  2. kristianmandrup created this gist Nov 13, 2018.
    87 changes: 87 additions & 0 deletions mockMang.js
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,87 @@
    import { makeExecutableSchema, addMockFunctionsToSchema } from "graphql-tools";
    import { graphql } from "graphql";
    import { ApolloClient } from "apollo-client";
    import { from } from "apollo-link";
    import gql from "graphql-tag";
    import { withClientState } from "apollo-link-state";
    import { InMemoryCache } from "apollo-cache-memory";
    import GoalsSchema from "../api/goals/Goal.graphql";

    // import { defaultState } from '../ui/config/apollo/defaultState'
    const defaultState = {};

    // import { defaultState } from '../ui/config/apollo/stateMutations'
    const stateMutations = {};

    const stateLink = withClientState({
    cache: new InMemoryCache(),
    resolvers: stateMutations,
    defaults: defaultState
    });

    // client.onResetStore(stateLink.writeDefaults)

    const link = from({ stateLink });

    const client = new ApolloClient({
    link,
    cache: new InMemoryCache()
    });

    const typeDefs = [GoalsSchema];

    const schema = makeExecutableSchema({ typeDefs });

    const revisedRandId = () =>
    Math.random()
    .toString(36)
    .replace(/[^a-z]+/g, "")
    .substr(2, 10);

    // mock order
    const order = {

    }
    // mock user
    const user = {

    }

    const product = {

    }

    const allProducts = [product]

    const mocks = {
    ID: () => revisedRandId(),
    Product: () => ({
    ...product
    }),
    Order: () => ({
    ...order
    }),
    User: () => ({
    ...user
    })
    }

    addMockFunctionsToSchema({schema, mocks})

    const mockMang = await (query, args = {}) => {
    try {
    const res = await graphql(schema, query.loc.source.body, null, null, args)
    res.data.loading = false
    return res.data
    } catch (err) {
    return console.log(err.message)
    }
    }

    export { client }

    export const mockFunc = () => null

    export const mockAsyncFunc = async () => true

    export default mockMang