Graphql Dataloader File Structure and ContextWriting files in Node.jsCheck synchronously if file/directory exists in Node.jsWhat's the difference between dependencies, devDependencies and peerDependencies in npm package.json file?Do all nodes in a relay-compliant GraphQL server need to have an id?Apollo GraphQL server; setting context to handle requests triggered by a fired subscriptionWrong order of GraphQL resolver arguments (root, args, context)GraphQL Dataloader not knowing keys in advanceGraphQL DataLoader - Loader per Service Layer vs Per ResolverHow to use Mongoose with GraphQL and DataLoader?Writing direct queries under graphql layer

How is it believable that Euron could so easily pull off this ambush?

Employee is self-centered and affects the team negatively

Antivirus for Ubuntu 18.04

What's the 2-minute timer on mobile Deutsche Bahn tickets?

Appropriate age to involve kids in life changing decisions

Which "exotic salt" can lower water's freezing point by 70 °C?

An adjective or a noun to describe a very small apartment / house etc

My C Drive is full without reason

How do I give a darkroom course without negs from the attendees?

Are modes in jazz primarily a melody thing?

Did Ham the Chimp follow commands, or did he just randomly push levers?

Assuming a normal distribution: what is the sd for a given mean?

Splitting polygons and dividing attribute value proportionally using ArcGIS Pro?

How does jetBlue determine its boarding order?

Explaining intravenous drug abuse to a small child

Why did Dr. Strange keep looking into the future after the snap?

Is it safe to keep the GPU on 100% utilization for a very long time?

Is throwing dice a stochastic or a deterministic process?

What’s the interaction between darkvision and the Eagle Aspect of the beast, if you have Darkvision past 100 feet?

Can a player choose to add detail and flavor to their character's spells and abilities?

The unknown and unexplained in science fiction

Why were the rules for Proliferate changed?

What's weird about Proto-Indo-European Stops?

Why can’t you see at the start of the Big Bang?



Graphql Dataloader File Structure and Context


Writing files in Node.jsCheck synchronously if file/directory exists in Node.jsWhat's the difference between dependencies, devDependencies and peerDependencies in npm package.json file?Do all nodes in a relay-compliant GraphQL server need to have an id?Apollo GraphQL server; setting context to handle requests triggered by a fired subscriptionWrong order of GraphQL resolver arguments (root, args, context)GraphQL Dataloader not knowing keys in advanceGraphQL DataLoader - Loader per Service Layer vs Per ResolverHow to use Mongoose with GraphQL and DataLoader?Writing direct queries under graphql layer






.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty height:90px;width:728px;box-sizing:border-box;








1















Let me preface this by saying I am not a javascript developer, so I'm probably missing something very obvious. I'm a data warehouse developer and creating a graphql server that can communicate with our DW got dropped in my lap.



I've been trying to get dataloaders to work on my graphql server by using a single object in the context, containing multiple dataloaders. I'm then trying to call the appropriate dataloader in the resolver. However, I've been unable to get this to work correctly. The consolidated dataloader object only works if I individually reference the dataloaders in the server context.



I'm trying to follow a similar pattern with the loaders as I have with my models, which is each broken out into a separate file, then consolidated for use as a single object via recursion through the file structure.



Example is I have an object called loaders which contains two loaders: countryLoader and marketsectorLoader, each of which is defined in a separate file under the "loaders" directory. In my server context, the following works



import * as loaders from "./loaders" 
graphQLServer.use('/graphql', bodyParser.json(),
graphqlExpress({
schema,
context: {
countryLoader: loaders.countryLoader()


I can then call this in my resolver:



 StateProvince: 
Country: (parent, args, countryLoader) =>
countryLoader.load(parent.Country_fkey) ,


This functions correctly, batching and returning the correct query result, but I'd prefer not to have to declare each specific dataloader from the loaders object as part of the context. However, I've been unable to figure out the syntax to use the loaders object in the context and call the appropriate
individual dataloader in the appropriate resolver.



I've tried several variants of the following example:
https://github.com/relay-tools/react-relay-network-layer/blob/master/examples/dataLoaderPerBatchRequest.js



which seems to be using the type of technique I'm trying to leverage:



//context snippet:
context:
request: req, // just for example, pass request to context
dataLoaders: initDataLoaders(),
,


However, no luck. I suspect the issue is with my resolver syntax, but I'm not sure, and I haven't been able to find working examples with multiple dataloaders.










share|improve this question




























    1















    Let me preface this by saying I am not a javascript developer, so I'm probably missing something very obvious. I'm a data warehouse developer and creating a graphql server that can communicate with our DW got dropped in my lap.



    I've been trying to get dataloaders to work on my graphql server by using a single object in the context, containing multiple dataloaders. I'm then trying to call the appropriate dataloader in the resolver. However, I've been unable to get this to work correctly. The consolidated dataloader object only works if I individually reference the dataloaders in the server context.



    I'm trying to follow a similar pattern with the loaders as I have with my models, which is each broken out into a separate file, then consolidated for use as a single object via recursion through the file structure.



    Example is I have an object called loaders which contains two loaders: countryLoader and marketsectorLoader, each of which is defined in a separate file under the "loaders" directory. In my server context, the following works



    import * as loaders from "./loaders" 
    graphQLServer.use('/graphql', bodyParser.json(),
    graphqlExpress({
    schema,
    context: {
    countryLoader: loaders.countryLoader()


    I can then call this in my resolver:



     StateProvince: 
    Country: (parent, args, countryLoader) =>
    countryLoader.load(parent.Country_fkey) ,


    This functions correctly, batching and returning the correct query result, but I'd prefer not to have to declare each specific dataloader from the loaders object as part of the context. However, I've been unable to figure out the syntax to use the loaders object in the context and call the appropriate
    individual dataloader in the appropriate resolver.



    I've tried several variants of the following example:
    https://github.com/relay-tools/react-relay-network-layer/blob/master/examples/dataLoaderPerBatchRequest.js



    which seems to be using the type of technique I'm trying to leverage:



    //context snippet:
    context:
    request: req, // just for example, pass request to context
    dataLoaders: initDataLoaders(),
    ,


    However, no luck. I suspect the issue is with my resolver syntax, but I'm not sure, and I haven't been able to find working examples with multiple dataloaders.










    share|improve this question
























      1












      1








      1








      Let me preface this by saying I am not a javascript developer, so I'm probably missing something very obvious. I'm a data warehouse developer and creating a graphql server that can communicate with our DW got dropped in my lap.



      I've been trying to get dataloaders to work on my graphql server by using a single object in the context, containing multiple dataloaders. I'm then trying to call the appropriate dataloader in the resolver. However, I've been unable to get this to work correctly. The consolidated dataloader object only works if I individually reference the dataloaders in the server context.



      I'm trying to follow a similar pattern with the loaders as I have with my models, which is each broken out into a separate file, then consolidated for use as a single object via recursion through the file structure.



      Example is I have an object called loaders which contains two loaders: countryLoader and marketsectorLoader, each of which is defined in a separate file under the "loaders" directory. In my server context, the following works



      import * as loaders from "./loaders" 
      graphQLServer.use('/graphql', bodyParser.json(),
      graphqlExpress({
      schema,
      context: {
      countryLoader: loaders.countryLoader()


      I can then call this in my resolver:



       StateProvince: 
      Country: (parent, args, countryLoader) =>
      countryLoader.load(parent.Country_fkey) ,


      This functions correctly, batching and returning the correct query result, but I'd prefer not to have to declare each specific dataloader from the loaders object as part of the context. However, I've been unable to figure out the syntax to use the loaders object in the context and call the appropriate
      individual dataloader in the appropriate resolver.



      I've tried several variants of the following example:
      https://github.com/relay-tools/react-relay-network-layer/blob/master/examples/dataLoaderPerBatchRequest.js



      which seems to be using the type of technique I'm trying to leverage:



      //context snippet:
      context:
      request: req, // just for example, pass request to context
      dataLoaders: initDataLoaders(),
      ,


      However, no luck. I suspect the issue is with my resolver syntax, but I'm not sure, and I haven't been able to find working examples with multiple dataloaders.










      share|improve this question














      Let me preface this by saying I am not a javascript developer, so I'm probably missing something very obvious. I'm a data warehouse developer and creating a graphql server that can communicate with our DW got dropped in my lap.



      I've been trying to get dataloaders to work on my graphql server by using a single object in the context, containing multiple dataloaders. I'm then trying to call the appropriate dataloader in the resolver. However, I've been unable to get this to work correctly. The consolidated dataloader object only works if I individually reference the dataloaders in the server context.



      I'm trying to follow a similar pattern with the loaders as I have with my models, which is each broken out into a separate file, then consolidated for use as a single object via recursion through the file structure.



      Example is I have an object called loaders which contains two loaders: countryLoader and marketsectorLoader, each of which is defined in a separate file under the "loaders" directory. In my server context, the following works



      import * as loaders from "./loaders" 
      graphQLServer.use('/graphql', bodyParser.json(),
      graphqlExpress({
      schema,
      context: {
      countryLoader: loaders.countryLoader()


      I can then call this in my resolver:



       StateProvince: 
      Country: (parent, args, countryLoader) =>
      countryLoader.load(parent.Country_fkey) ,


      This functions correctly, batching and returning the correct query result, but I'd prefer not to have to declare each specific dataloader from the loaders object as part of the context. However, I've been unable to figure out the syntax to use the loaders object in the context and call the appropriate
      individual dataloader in the appropriate resolver.



      I've tried several variants of the following example:
      https://github.com/relay-tools/react-relay-network-layer/blob/master/examples/dataLoaderPerBatchRequest.js



      which seems to be using the type of technique I'm trying to leverage:



      //context snippet:
      context:
      request: req, // just for example, pass request to context
      dataLoaders: initDataLoaders(),
      ,


      However, no luck. I suspect the issue is with my resolver syntax, but I'm not sure, and I haven't been able to find working examples with multiple dataloaders.







      node.js graphql






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Mar 22 at 20:09









      ListerDListerD

      215




      215






















          1 Answer
          1






          active

          oldest

          votes


















          0














          If I'm reading your code correctly, importing your loaders using a wildcard import like this:



          import * as loaders from './loaders'


          results in an object wherein each property is a function that creates an instance of a particular DataLoader. So we just need to iterate through each property. For example:



          // Using forEach
          const dataLoaders =
          Object.keys(loaders).forEach(loaderName =>
          dataLoaders[loaderName] = loaders[loaderName]()
          )

          // Or using reduce
          const dataLoaders = Object.keys(loaders).reduce((result, loaderName) =>
          result[loaderName] = loaders[loaderName]()
          return result
          , )


          Using lodash, you can also just do something like:



          const dataLoaders = _.mapValues(loaders, loader => loader())





          share|improve this answer























          • Thanks Daniel, I'll try to integrate this into my code. The major issue I'm having is getting it to recognize the loaders in the server context without them being explicitly called, so I'll try the different iteration methods you've outlined.

            – ListerD
            Mar 25 at 12:58











          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%2f55307130%2fgraphql-dataloader-file-structure-and-context%23new-answer', 'question_page');

          );

          Post as a guest















          Required, but never shown

























          1 Answer
          1






          active

          oldest

          votes








          1 Answer
          1






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes









          0














          If I'm reading your code correctly, importing your loaders using a wildcard import like this:



          import * as loaders from './loaders'


          results in an object wherein each property is a function that creates an instance of a particular DataLoader. So we just need to iterate through each property. For example:



          // Using forEach
          const dataLoaders =
          Object.keys(loaders).forEach(loaderName =>
          dataLoaders[loaderName] = loaders[loaderName]()
          )

          // Or using reduce
          const dataLoaders = Object.keys(loaders).reduce((result, loaderName) =>
          result[loaderName] = loaders[loaderName]()
          return result
          , )


          Using lodash, you can also just do something like:



          const dataLoaders = _.mapValues(loaders, loader => loader())





          share|improve this answer























          • Thanks Daniel, I'll try to integrate this into my code. The major issue I'm having is getting it to recognize the loaders in the server context without them being explicitly called, so I'll try the different iteration methods you've outlined.

            – ListerD
            Mar 25 at 12:58















          0














          If I'm reading your code correctly, importing your loaders using a wildcard import like this:



          import * as loaders from './loaders'


          results in an object wherein each property is a function that creates an instance of a particular DataLoader. So we just need to iterate through each property. For example:



          // Using forEach
          const dataLoaders =
          Object.keys(loaders).forEach(loaderName =>
          dataLoaders[loaderName] = loaders[loaderName]()
          )

          // Or using reduce
          const dataLoaders = Object.keys(loaders).reduce((result, loaderName) =>
          result[loaderName] = loaders[loaderName]()
          return result
          , )


          Using lodash, you can also just do something like:



          const dataLoaders = _.mapValues(loaders, loader => loader())





          share|improve this answer























          • Thanks Daniel, I'll try to integrate this into my code. The major issue I'm having is getting it to recognize the loaders in the server context without them being explicitly called, so I'll try the different iteration methods you've outlined.

            – ListerD
            Mar 25 at 12:58













          0












          0








          0







          If I'm reading your code correctly, importing your loaders using a wildcard import like this:



          import * as loaders from './loaders'


          results in an object wherein each property is a function that creates an instance of a particular DataLoader. So we just need to iterate through each property. For example:



          // Using forEach
          const dataLoaders =
          Object.keys(loaders).forEach(loaderName =>
          dataLoaders[loaderName] = loaders[loaderName]()
          )

          // Or using reduce
          const dataLoaders = Object.keys(loaders).reduce((result, loaderName) =>
          result[loaderName] = loaders[loaderName]()
          return result
          , )


          Using lodash, you can also just do something like:



          const dataLoaders = _.mapValues(loaders, loader => loader())





          share|improve this answer













          If I'm reading your code correctly, importing your loaders using a wildcard import like this:



          import * as loaders from './loaders'


          results in an object wherein each property is a function that creates an instance of a particular DataLoader. So we just need to iterate through each property. For example:



          // Using forEach
          const dataLoaders =
          Object.keys(loaders).forEach(loaderName =>
          dataLoaders[loaderName] = loaders[loaderName]()
          )

          // Or using reduce
          const dataLoaders = Object.keys(loaders).reduce((result, loaderName) =>
          result[loaderName] = loaders[loaderName]()
          return result
          , )


          Using lodash, you can also just do something like:



          const dataLoaders = _.mapValues(loaders, loader => loader())






          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Mar 23 at 6:01









          Daniel ReardenDaniel Rearden

          18.7k12247




          18.7k12247












          • Thanks Daniel, I'll try to integrate this into my code. The major issue I'm having is getting it to recognize the loaders in the server context without them being explicitly called, so I'll try the different iteration methods you've outlined.

            – ListerD
            Mar 25 at 12:58

















          • Thanks Daniel, I'll try to integrate this into my code. The major issue I'm having is getting it to recognize the loaders in the server context without them being explicitly called, so I'll try the different iteration methods you've outlined.

            – ListerD
            Mar 25 at 12:58
















          Thanks Daniel, I'll try to integrate this into my code. The major issue I'm having is getting it to recognize the loaders in the server context without them being explicitly called, so I'll try the different iteration methods you've outlined.

          – ListerD
          Mar 25 at 12:58





          Thanks Daniel, I'll try to integrate this into my code. The major issue I'm having is getting it to recognize the loaders in the server context without them being explicitly called, so I'll try the different iteration methods you've outlined.

          – ListerD
          Mar 25 at 12:58



















          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%2f55307130%2fgraphql-dataloader-file-structure-and-context%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







          Popular posts from this blog

          Kamusi Yaliyomo Aina za kamusi | Muundo wa kamusi | Faida za kamusi | Dhima ya picha katika kamusi | Marejeo | Tazama pia | Viungo vya nje | UrambazajiKuhusu kamusiGo-SwahiliWiki-KamusiKamusi ya Kiswahili na Kiingerezakuihariri na kuongeza habari

          SQL error code 1064 with creating Laravel foreign keysForeign key constraints: When to use ON UPDATE and ON DELETEDropping column with foreign key Laravel error: General error: 1025 Error on renameLaravel SQL Can't create tableLaravel Migration foreign key errorLaravel php artisan migrate:refresh giving a syntax errorSQLSTATE[42S01]: Base table or view already exists or Base table or view already exists: 1050 Tableerror in migrating laravel file to xampp serverSyntax error or access violation: 1064:syntax to use near 'unsigned not null, modelName varchar(191) not null, title varchar(191) not nLaravel cannot create new table field in mysqlLaravel 5.7:Last migration creates table but is not registered in the migration table

          은진 송씨 목차 역사 본관 분파 인물 조선 왕실과의 인척 관계 집성촌 항렬자 인구 같이 보기 각주 둘러보기 메뉴은진 송씨세종실록 149권, 지리지 충청도 공주목 은진현