dfernandeza / graphql-mimic

Client side mocking library for GraphQL.
4 stars 0 forks source link

GraphQL client agnostic? #1

Open charlypoly opened 4 years ago

charlypoly commented 4 years ago

@dfernandeza 👋

Is graphql-mimic client agnostic or should we provide "adapter" for Apollo first with a clear interface - if possible - for people wanting to use it with other clients?

I am asking because I would see myself wanting to mock existing query in my application components that rely a lot on Apollo's useQuery(), etc

Example usage with Apollo (using your README example):

myComponent.ts

const QUERY = gql`
  query {
    characters {
      results {
        id
        name
      }
    }
  }
`;

const MyComponent = () => {
   const { data } = useQuery(QUERY)
   return (
      <div>
         {data.characters.results.map((c) => <div className="name">${c.name}</div>)}
      </div>
   )
}

myComponent.spec.ts

import { shallow } from 'enzyme';
import * as schema from "./schema.json";
import MyComponent from 'src/MyComponent"

// <MimicProvider> provides a test <ApolloProvider> for testing
const MimicProvider = Mimic.Provider({ schema });

describe('MyComponent', () => {
   it('renders 2 items', () => {
      const wrapper = shallow(<MimicProvider><MyComponent /></MimicProvider>);
      expect(wrapper.find('.name')).to.have.lengthOf(2);
   });  
})
dfernandeza commented 4 years ago

Hey @wittydeveloper,

Thanks, I really appreciate this feedback.

I think we are pretty much in the same page, in fact my first thought was actually to make it work specifically for Apollo Client setups, and I'd like to focus on this use-case first and use this as a baseline going forward.

Cheers

charlypoly commented 4 years ago

@dfernandeza

I'll see if we can end up with a nicer API than <MimicProvider><MyComponent /></MimicProvider>

dfernandeza commented 4 years ago

I'll be working on a first draft of the main functionality (the one described on the test file) this week.