How to partially mock a schema in GraphQL and Apollo?










1















I'm trying to mock only a small portion of the Contact type below. My resolvers return data from a REST endpoint for all fields in Contact except for test. For demo purposes, I want to be able to retain the server data for all other fields, but only mock the test field.



I have the following GraphQL schema defined:



const typeDefs = `
type Contact
id: String,
first_name: String
last_name: String
middle_name: String
date_of_birth: String
test: String


type Query
contacts: [Contact!]!
Contact(id: String!): Contact!

`;


I have the following mocks defined:



const mocks = 
Contact: () => (
test: () => "This data is mocked!"
)
;


And the following resolvers defined:



const resolvers = 
Query:
contacts: async (parent, args, dataSources ) =>
dataSources.MYAPI.getAllContacts(),
Contact: async (parent, id , dataSources ) =>
dataSources.MYAPI.getContact(id)

;


Then I initialize the server with:



const server = new ApolloServer(
typeDefs,
resolvers,
mocks,
dataSources: () =>
return
MYAPI: new MYAPI()
;
,
mockEntireSchema: false
);


The above does not work. I added the mockEntireSchema:true configuration which prevented my server response from being overridden, but the test attribute still returns the default String mock of Hello World instead of my attempted mock of This data is mocked!.



I know the mock is set up correctly because I can remove the mockEntireSchema config and my mock data appears correctly.



Is this even possible or does the behavior of mockEntireSchema and mocks in general not support this?










share|improve this question


























    1















    I'm trying to mock only a small portion of the Contact type below. My resolvers return data from a REST endpoint for all fields in Contact except for test. For demo purposes, I want to be able to retain the server data for all other fields, but only mock the test field.



    I have the following GraphQL schema defined:



    const typeDefs = `
    type Contact
    id: String,
    first_name: String
    last_name: String
    middle_name: String
    date_of_birth: String
    test: String


    type Query
    contacts: [Contact!]!
    Contact(id: String!): Contact!

    `;


    I have the following mocks defined:



    const mocks = 
    Contact: () => (
    test: () => "This data is mocked!"
    )
    ;


    And the following resolvers defined:



    const resolvers = 
    Query:
    contacts: async (parent, args, dataSources ) =>
    dataSources.MYAPI.getAllContacts(),
    Contact: async (parent, id , dataSources ) =>
    dataSources.MYAPI.getContact(id)

    ;


    Then I initialize the server with:



    const server = new ApolloServer(
    typeDefs,
    resolvers,
    mocks,
    dataSources: () =>
    return
    MYAPI: new MYAPI()
    ;
    ,
    mockEntireSchema: false
    );


    The above does not work. I added the mockEntireSchema:true configuration which prevented my server response from being overridden, but the test attribute still returns the default String mock of Hello World instead of my attempted mock of This data is mocked!.



    I know the mock is set up correctly because I can remove the mockEntireSchema config and my mock data appears correctly.



    Is this even possible or does the behavior of mockEntireSchema and mocks in general not support this?










    share|improve this question
























      1












      1








      1








      I'm trying to mock only a small portion of the Contact type below. My resolvers return data from a REST endpoint for all fields in Contact except for test. For demo purposes, I want to be able to retain the server data for all other fields, but only mock the test field.



      I have the following GraphQL schema defined:



      const typeDefs = `
      type Contact
      id: String,
      first_name: String
      last_name: String
      middle_name: String
      date_of_birth: String
      test: String


      type Query
      contacts: [Contact!]!
      Contact(id: String!): Contact!

      `;


      I have the following mocks defined:



      const mocks = 
      Contact: () => (
      test: () => "This data is mocked!"
      )
      ;


      And the following resolvers defined:



      const resolvers = 
      Query:
      contacts: async (parent, args, dataSources ) =>
      dataSources.MYAPI.getAllContacts(),
      Contact: async (parent, id , dataSources ) =>
      dataSources.MYAPI.getContact(id)

      ;


      Then I initialize the server with:



      const server = new ApolloServer(
      typeDefs,
      resolvers,
      mocks,
      dataSources: () =>
      return
      MYAPI: new MYAPI()
      ;
      ,
      mockEntireSchema: false
      );


      The above does not work. I added the mockEntireSchema:true configuration which prevented my server response from being overridden, but the test attribute still returns the default String mock of Hello World instead of my attempted mock of This data is mocked!.



      I know the mock is set up correctly because I can remove the mockEntireSchema config and my mock data appears correctly.



      Is this even possible or does the behavior of mockEntireSchema and mocks in general not support this?










      share|improve this question














      I'm trying to mock only a small portion of the Contact type below. My resolvers return data from a REST endpoint for all fields in Contact except for test. For demo purposes, I want to be able to retain the server data for all other fields, but only mock the test field.



      I have the following GraphQL schema defined:



      const typeDefs = `
      type Contact
      id: String,
      first_name: String
      last_name: String
      middle_name: String
      date_of_birth: String
      test: String


      type Query
      contacts: [Contact!]!
      Contact(id: String!): Contact!

      `;


      I have the following mocks defined:



      const mocks = 
      Contact: () => (
      test: () => "This data is mocked!"
      )
      ;


      And the following resolvers defined:



      const resolvers = 
      Query:
      contacts: async (parent, args, dataSources ) =>
      dataSources.MYAPI.getAllContacts(),
      Contact: async (parent, id , dataSources ) =>
      dataSources.MYAPI.getContact(id)

      ;


      Then I initialize the server with:



      const server = new ApolloServer(
      typeDefs,
      resolvers,
      mocks,
      dataSources: () =>
      return
      MYAPI: new MYAPI()
      ;
      ,
      mockEntireSchema: false
      );


      The above does not work. I added the mockEntireSchema:true configuration which prevented my server response from being overridden, but the test attribute still returns the default String mock of Hello World instead of my attempted mock of This data is mocked!.



      I know the mock is set up correctly because I can remove the mockEntireSchema config and my mock data appears correctly.



      Is this even possible or does the behavior of mockEntireSchema and mocks in general not support this?







      graphql apollo apollo-server






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Nov 14 '18 at 16:57









      Tom NolanTom Nolan

      99231437




      99231437






















          2 Answers
          2






          active

          oldest

          votes


















          1














          According to the documentation, you want to keep mockEntireSchema as false and create a mocks object that has the components in it that you still WANT to mock. All other resolvers will be used as they exist. Any mocks that you HAVE defined, though, will be used, so the query resolvers that return Contact types will never be used, since you have defined Contact as a mock.






          share|improve this answer






























            0














            I haven't tried it but to me it seems like it should work (what you describe). But since it doesn't, a possible workaround would be to just add a field resolver for the test field:



            const resolvers = 
            Contact:
            test: () => "This data is not mocked but the effect is the same!"
            ,
            Query:
            contacts: async (parent, args, dataSources ) =>
            dataSources.MYAPI.getAllContacts(),
            Contact: async (parent, id , dataSources ) =>
            dataSources.MYAPI.getContact(id)

            ;





            share|improve this answer
























              Your Answer






              StackExchange.ifUsing("editor", function ()
              StackExchange.using("externalEditor", function ()
              StackExchange.using("snippets", function ()
              StackExchange.snippets.init();
              );
              );
              , "code-snippets");

              StackExchange.ready(function()
              var channelOptions =
              tags: "".split(" "),
              id: "1"
              ;
              initTagRenderer("".split(" "), "".split(" "), channelOptions);

              StackExchange.using("externalEditor", function()
              // Have to fire editor after snippets, if snippets enabled
              if (StackExchange.settings.snippets.snippetsEnabled)
              StackExchange.using("snippets", function()
              createEditor();
              );

              else
              createEditor();

              );

              function createEditor()
              StackExchange.prepareEditor(
              heartbeatType: 'answer',
              autoActivateHeartbeat: false,
              convertImagesToLinks: true,
              noModals: true,
              showLowRepImageUploadWarning: true,
              reputationToPostImages: 10,
              bindNavPrevention: true,
              postfix: "",
              imageUploader:
              brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
              contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
              allowUrls: true
              ,
              onDemand: true,
              discardSelector: ".discard-answer"
              ,immediatelyShowMarkdownHelp:true
              );



              );













              draft saved

              draft discarded


















              StackExchange.ready(
              function ()
              StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53305234%2fhow-to-partially-mock-a-schema-in-graphql-and-apollo%23new-answer', 'question_page');

              );

              Post as a guest















              Required, but never shown

























              2 Answers
              2






              active

              oldest

              votes








              2 Answers
              2






              active

              oldest

              votes









              active

              oldest

              votes






              active

              oldest

              votes









              1














              According to the documentation, you want to keep mockEntireSchema as false and create a mocks object that has the components in it that you still WANT to mock. All other resolvers will be used as they exist. Any mocks that you HAVE defined, though, will be used, so the query resolvers that return Contact types will never be used, since you have defined Contact as a mock.






              share|improve this answer



























                1














                According to the documentation, you want to keep mockEntireSchema as false and create a mocks object that has the components in it that you still WANT to mock. All other resolvers will be used as they exist. Any mocks that you HAVE defined, though, will be used, so the query resolvers that return Contact types will never be used, since you have defined Contact as a mock.






                share|improve this answer

























                  1












                  1








                  1







                  According to the documentation, you want to keep mockEntireSchema as false and create a mocks object that has the components in it that you still WANT to mock. All other resolvers will be used as they exist. Any mocks that you HAVE defined, though, will be used, so the query resolvers that return Contact types will never be used, since you have defined Contact as a mock.






                  share|improve this answer













                  According to the documentation, you want to keep mockEntireSchema as false and create a mocks object that has the components in it that you still WANT to mock. All other resolvers will be used as they exist. Any mocks that you HAVE defined, though, will be used, so the query resolvers that return Contact types will never be used, since you have defined Contact as a mock.







                  share|improve this answer












                  share|improve this answer



                  share|improve this answer










                  answered Nov 19 '18 at 18:35









                  Dan CrewsDan Crews

                  1,5841019




                  1,5841019























                      0














                      I haven't tried it but to me it seems like it should work (what you describe). But since it doesn't, a possible workaround would be to just add a field resolver for the test field:



                      const resolvers = 
                      Contact:
                      test: () => "This data is not mocked but the effect is the same!"
                      ,
                      Query:
                      contacts: async (parent, args, dataSources ) =>
                      dataSources.MYAPI.getAllContacts(),
                      Contact: async (parent, id , dataSources ) =>
                      dataSources.MYAPI.getContact(id)

                      ;





                      share|improve this answer





























                        0














                        I haven't tried it but to me it seems like it should work (what you describe). But since it doesn't, a possible workaround would be to just add a field resolver for the test field:



                        const resolvers = 
                        Contact:
                        test: () => "This data is not mocked but the effect is the same!"
                        ,
                        Query:
                        contacts: async (parent, args, dataSources ) =>
                        dataSources.MYAPI.getAllContacts(),
                        Contact: async (parent, id , dataSources ) =>
                        dataSources.MYAPI.getContact(id)

                        ;





                        share|improve this answer



























                          0












                          0








                          0







                          I haven't tried it but to me it seems like it should work (what you describe). But since it doesn't, a possible workaround would be to just add a field resolver for the test field:



                          const resolvers = 
                          Contact:
                          test: () => "This data is not mocked but the effect is the same!"
                          ,
                          Query:
                          contacts: async (parent, args, dataSources ) =>
                          dataSources.MYAPI.getAllContacts(),
                          Contact: async (parent, id , dataSources ) =>
                          dataSources.MYAPI.getContact(id)

                          ;





                          share|improve this answer















                          I haven't tried it but to me it seems like it should work (what you describe). But since it doesn't, a possible workaround would be to just add a field resolver for the test field:



                          const resolvers = 
                          Contact:
                          test: () => "This data is not mocked but the effect is the same!"
                          ,
                          Query:
                          contacts: async (parent, args, dataSources ) =>
                          dataSources.MYAPI.getAllContacts(),
                          Contact: async (parent, id , dataSources ) =>
                          dataSources.MYAPI.getContact(id)

                          ;






                          share|improve this answer














                          share|improve this answer



                          share|improve this answer








                          edited Nov 19 '18 at 21:17

























                          answered Nov 15 '18 at 23:18









                          Mikael LirbankMikael Lirbank

                          1,1121011




                          1,1121011



























                              draft saved

                              draft discarded
















































                              Thanks for contributing an answer to Stack Overflow!


                              • Please be sure to answer the question. Provide details and share your research!

                              But avoid


                              • Asking for help, clarification, or responding to other answers.

                              • Making statements based on opinion; back them up with references or personal experience.

                              To learn more, see our tips on writing great answers.




                              draft saved


                              draft discarded














                              StackExchange.ready(
                              function ()
                              StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53305234%2fhow-to-partially-mock-a-schema-in-graphql-and-apollo%23new-answer', 'question_page');

                              );

                              Post as a guest















                              Required, but never shown





















































                              Required, but never shown














                              Required, but never shown












                              Required, but never shown







                              Required, but never shown

































                              Required, but never shown














                              Required, but never shown












                              Required, but never shown







                              Required, but never shown







                              這個網誌中的熱門文章

                              How to read a connectionString WITH PROVIDER in .NET Core?

                              In R, how to develop a multiplot heatmap.2 figure showing key labels successfully

                              Museum of Modern and Contemporary Art of Trento and Rovereto