Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Importing providers with configurations without merging Query schemas #264

Closed
gnz00 opened this issue Jan 12, 2019 · 1 comment
Closed

Comments

@gnz00
Copy link

gnz00 commented Jan 12, 2019

Suppose you have a simple module with a provider that relies on an injected configuration:

// ProviderA.ts
import { Injectable, Inject } from "@graphql-modules/di"
import { ModuleConfig } from '@graphql-modules/core'
import "reflect-metadata"

@Injectable()
export class ProviderA {
    constructor(@Inject(ModuleConfig('module-a')) private config: Config) {}

    withIds(ids: string[]): String[] {
        return [];
    }
}
// ModuleA.ts
import { GraphQLModule, ModuleContext } from '@graphql-modules/core'
import { ProviderA } from './ProviderA'
import gql from 'graphql-tag'

export const ModuleA = new GraphQLModule<ModuleAConfig>({
    name: 'module-a',
    providers: () => [ProviderA],
    typeDefs: gql`
        type Query {
            people:(ids: [ID]!): [String]
        }
    `,
    resolvers: {
        Query: {
            people: (_, {ids}, {injector}: ModuleContext) => injector.get(ProviderA).withIds(ids)
        }
    }
})

And you try to create a higher order module that utilizes ProviderA:

// ModuleB.ts
import { GraphQLModule } from '@graphql-modules/core';
import { ModuleA, ModuleAConfig } from './ModuleA'

export interface ModuleBConfig {
    moduleA: ModuleAConfig
}

export const ModuleB = new GraphQLModule<ModuleBConfig>({
    name: 'module-b',
    imports: ({config}) => [ModuleA.forRoot(config.moduleA)],
    providers: () => [ProviderA],
    typeDefs: gql`
        type Query {
            composed(ids: [ID]!): [String]
        }
    `,
    resolvers: {
        Query: {
            composed: (_, {ids}, {injector}: ModuleContext) => {
                return injector.get(ProviderA).withIds(ids).map(
                    // transform...
                )
            }
        }
    }
})

With the above setup, composed and people will both be exposed as queries. Is there a way to only import the providers and pass them configuration?

@ardatan
Copy link
Collaborator

ardatan commented Jan 12, 2019

You can create a module that only has providers, and that module is imported by the other two modules so providers are shared by two modules without merging the schema.

@ardatan ardatan closed this as completed Jan 14, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants