salesforce / lwc

⚡️ LWC - A Blazing Fast, Enterprise-Grade Web Components Foundation
https://lwc.dev
Other
1.64k stars 392 forks source link

Create graphql loader for lwc #3581

Open AllanOricil opened 1 year ago

AllanOricil commented 1 year ago

Is your feature request related to a problem? Please describe.

Writing graphql queries in components is too verbose. It could also lead to code duplication if multiple components need the same query with different sets of variables.

https://developer.salesforce.com/docs/component-library/documentation/en/lwc/lwc.reference_graphql

Describe the solution you'd like

.graphql files could be bundled with lightning components during deployments.

force-app
└── main
    └── default 
        └── lwc
            └── myComponent
                ├── myComponent.js 
                ├── myComponent.xml 
                └── myQuery.graphql
import { lightningComponent, wire } from 'lwc'
import { graphql } from 'lightning/uiGraphQLApi'
import myQuery from "myQuery.graphql"  // it would be necessary to create a gql loader like if we were using webpack

export default class MyComponent extends lightningComponent {

//this could also be a getter or a normal function
variables = {
   "KEY": "VALUE",
   ...
}

@wire(graphql, { 
    query: myQuery, 
    variables: $variables
})
someProperty
...

}

With .graphql files developers can:

It must also let developers to import .graphql from other bundles

import myQuery from "<namespace>/otherComponentBundle/myQuery.graphql"

Webpack graphql loader

https://www.npmjs.com/package/@graphql-tools/webpack-loader

https://www.npmjs.com/package/webpack-graphql-loader

https://medium.com/modus-create-front-end-development/load-your-graphql-documents-with-webpack-apollo-c1e166404cba

After releasing this loader I would also deprecate this inline graphql feature to encourage people to use .graphql files

Describe alternatives you've considered I could write my own loader

Or

As an alternative for modularization people can do the following

import  { gql } from "lightning/uiGraphQLApi"

export const myQuery = gql`...`;

then import myQuery in their components and pass its ref to wire. It is just really annoying having to import the gql module multiple times, specially when organizing queries in multiple modules, or the need to wrap the query in an object by hand.

Additional context N/A

AllanOricil commented 1 year ago

This loader would be useful for LWR projects as well

AllanOricil commented 1 year ago

The local dev server must also support this

AllanOricil commented 1 year ago

Look how horrible this looks 😅

SmartSelect_20230626_095447_LinkedIn.jpg

jmsjtu commented 1 year ago

Thanks for providing feedback @AllanOricil, this is part of Lightning Data Services.

I've tagged a few members of the team to take a look.

@scarrawaySF @jodarove