paljs / prisma-tools

Prisma tools to help you generate CRUD system for GraphQL servers
https://paljs.com
MIT License
687 stars 55 forks source link

filterInputs and excludeFields are not working. also would be great more info for filter #232

Closed terion-name closed 3 years ago

terion-name commented 3 years ago

v 3.8.3 I generate input file like this:

await fs.promises.writeFile(path.join(output, 'inputs.ts'), `import gql from 'graphql-tag'

export default gql\`
    ${print(sdlInputs({
                dmmf, 
                doNotUseFieldUpdateOperationsInput: true, 
                excludeFields: ['id', 'updatedAt', 'createdAt'],
                filterInputs: input => {
                    let f = input.fields.filter((field) => !['id', 'updatedAt', 'createdAt'].includes(field.name));
                    if (!f.length) { // sometimes there are entities with only these 3 feelds
                        return input.fields.filter((field) => !['updatedAt', 'createdAt'].includes(field.name))
                    }
                    return f;
                }
            }))

With any combination of excludeFields and filterInputs (anyone or both) — file output contains these fields.

Also, can callback get some more data for filtering? e.g. fields with @id, @updatedAt and @createdAt directives should be omitted in general (not by names, but by directives) and even by default I suppose, but there is no data on this in callback input. can it be passed to callback?

AhmedElywa commented 3 years ago

I will look at your code carefully, but you can not use the same props same time. The second one will rewrite the first one.

terion-name commented 3 years ago

@AhmedElywa as I've said I've tried all combos, none work

AhmedElywa commented 3 years ago

@terion-name after looking at my code. I exported the generateInputsString function in the last version 4.0.7 you can use to return a string direct you can use it to build your file without using the print function.

https://github.com/paljs/prisma-tools/blob/main/packages/plugins/src/sdlInputs.ts#L104-L99

terion-name commented 3 years ago

@AhmedElywa thank you) I'll test soon

terion-name commented 3 years ago

@AhmedElywa thank you, now it works. Added such filter to remove immutable fields:

filterInputs: input => {
        const omit = ['updatedAt', 'createdAt'];
        if (input.name.match(/.+(Create|Update)[A-Z].*/)) {
            omit.push('id')
        }
        return input.fields.filter((field) => !omit.includes(field.name));
    }

But this is far from perfect due to autogenerated primary key can be named not id :( it's strange that prisma's built-in inputs (that you use internally to render, if I understand correctly) include fields that should not be user-provided.