8000 Resolver of a nested Type is not triggered. · Issue #337 · graphql-compose/graphql-compose · GitHub
[go: up one dir, main page]

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

Resolver of a nested Type is not triggered. #337

Open
innovaweb-dev opened this issue May 26, 2021 · 1 comment
Open

Resolver of a nested Type is not triggered. #337

innovaweb-dev opened this issue May 26, 2021 · 1 comment

Comments

@innovaweb-dev
Copy link
innovaweb-dev commented May 26, 2021

AddressTC is a reusable Type, when UserTC or another Type is created or updated, I 973A whant to trigger validation automatically of AddressTC. If you know a better way to do that, I'll take it. With the code above, when I create or update User, resolever of AddressTC is not triggered.

// In this example I'll use UserTC but this can another type which also use AdressTC.

const UserTC = schemaComposer.createObjectTC({
    name: 'User',
    fields: {
        id: 'String',
        name: 'String',
        slug: 'String',
        actived: 'Boolean',
        registered: 'Boolean',
        address: AddressTC,
    }
});

const UserITC = UserTC.getInputTypeComposer()

UserTC.addResolver({
    kind: 'mutation',
    name: 'create',
    args: {
        data: UserITC
    },
    type: UserTC,
    resolve: async ({args: {data}, context}) => {
        // What I'll want it does : to trigger validation and geocode of AddressTC
        // What it does currently, GraphQL ignore AdressTC data and it just save data without validate
    },
})
// reusable Address Type

const AddressTC = schemaComposer.createObjectTC({
    name: 'Address',
    description: 'Type of address',
    fields: {
        street: 'String',
        number: 'String',
        postcode: 'String',
        city: 'String',
        comment: 'String',
        country: 'String',
        quality: QualityETC
    }
});

const AddressITC = AddressTC.getInputTypeComposer()

AddressTC.addResolver({
    kind: 'mutation',
    name: 'validation',
    args: {
        data: AddressITC
    },
    type: AddressTC,
    resolve: async ({args: {data}, context}) => {
        // When address is puted or updated :
        // => Make validation
        // => Geocode {Lat,Lng} with map provider 
        // Save in DB           
    },
})
@Michael-vdL
Copy link

Hello,

I believe you should look at the wrapped resolver methods in the docs.
Since you already have a validation resolver method, you should be able to do something along the lines of
const createUserWithValidation = UserTC.getResolver(‘create’).wrapResolver({ // call validation code here. })

Or, you can utilize middlewares (probably the correct approach). With middlewares, you can execute code before or after the resolver is executed. So you could write a middleware function that validates code prior to mutation.

`
function validateAddress () {
// validation code
}

const createUserWithValidation = UserTC.getResolver(‘create’).withMiddlewares([validateAddress]);
`

The bonus of the wrap method is you could make a fairly un-opinionated function that wrap all of your resolvers before they are built by schema composer and if the object contains a field of type AddressTC, it would enforce validation on all AddressTCs. With middlewares is probably cleaner.

Sorry for the lack of detail, I’m on iPad right now. If you need any additional information I can try to provide more detail later.

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
0