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

fix: plugins can now query the API without content-security-policy re… #8665

Merged
merged 2 commits into from Dec 8, 2021
Merged

Conversation

LeOndaz
Copy link
Contributor

@LeOndaz LeOndaz commented Nov 26, 2021

This is something tricky, plugins can now query the /graphql/ API. This update introduces the following pattern:

#  saleor/plugins/my_special_plugin/plugin.py
from .graphql.schema import schema  # schema created for the plugin 

class MPlugin(BasePlugin):
    ...

    def webhook(self, request, path, previous_value):
        view = GraphQLView.as_view(schema=schema)
        return view(request)

And plugins can have their endpoints on /plugins/plugin_id/

Then we can federate the plugins with the base GraphQL API.

@db-queries
Copy link

db-queries bot commented Nov 26, 2021

Here is the report for 4af780a (LeOndaz:main)
Base comparison is d38e3ae.

No differences were found.

# saleor.graphql.accountbenchmark account
  test name                                                               	left count 	right count	duplicate count
  ------------------------------------------------------------------------	-----------	-----------	---------------
  addresses for federation query count                                    	          9	          9	              2
  customers query                                                         	         48	         48	              0
  delete staff members                                                    	         34	         34	              0
  query staff user                                                        	         18	         18	              3
  staff create                                                            	         23	         23	              3
  staff update groups and permissions                                     	         33	         33	              5
  users for federation query count                                        	          8	          8	              3

# saleor.graphql.accountbenchmark permission group
  test name                                                               	left count 	right count	duplicate count
  ------------------------------------------------------------------------	-----------	-----------	---------------
  groups for federation query count                                       	          5	          5	              2
  permission group create                                                 	         19	         19	              2
  permission group delete                                                 	         20	         20	              3
  permission group query                                                  	          7	          7	              0
  permission group update                                                 	         33	         33	              1
  permission group update remove users with manage staff                  	         27	         27	              3

# saleor.graphql.appbenchmarks app extensions
  test name                                                               	left count 	right count	duplicate count
  ------------------------------------------------------------------------	-----------	-----------	---------------
  app extensions                                                          	         14	         14	              0
  app extensions with filter[filter0]                                     	         13	         13	              0
  app extensions with filter[filter1]                                     	         13	         13	              0
  app extensions with filter[filter2]                                     	          9	          9	              0
  app extensions with filter[filter3]                                     	         11	         11	              0
  app extensions with filter[filter4]                                     	         11	         11	              0
  app extensions with filter[filter5]                                     	          5	          5	              0
  app extensions with filter[filter6]                                     	          9	          9	              0

# saleor.graphql.appbenchmarks apps
  test name                                                               	left count 	right count	duplicate count
  ------------------------------------------------------------------------	-----------	-----------	---------------
  apps for federation query count                                         	          9	          9	              3

# saleor.graphql.attributebenchmark attribute
  test name                                                               	left count 	right count	duplicate count
  ------------------------------------------------------------------------	-----------	-----------	---------------
  attribute translation                                                   	          6	          6	              0
  attribute value translation                                             	         26	         26	              0
  query attribute                                                         	          6	          6	              0
  query attributes                                                        	          8	          8	              0

# saleor.graphql.checkoutbenchmark checkout mutations
  test name                                                               	left count 	right count	duplicate count
  ------------------------------------------------------------------------	-----------	-----------	---------------
  add billing address to checkout                                         	         51	         51	              5
  add checkout lines                                                      	         52	         52	              4
  add checkout lines with reservations                                    	        122	        122	             38
  add delivery to checkout                                                	         60	         60	              7
  add shipping to checkout                                                	         65	         65	              8
  checkout email update                                                   	         24	         24	              0
  checkout payment charge                                                 	         46	         46	             17
  checkout shipping address update                                        	         70	         70	             11
  checkout voucher code                                                   	         75	         75	             13
  complete checkout                                                       	        122	        122	             11
  complete checkout preorder                                              	        132	        132	             12
  complete checkout with out of stock webhook                             	        124	        124	             11
  complete checkout with single line                                      	        124	        124	             11
  create checkout                                                         	         72	         72	             10
  create checkout for cc                                                  	         63	         63	              8
  create checkout with reservations                                       	        117	        117	             21
  customer complete checkout                                              	        165	        165	             37
  customer complete checkout for cc                                       	        164	        164	             38
  update checkout lines                                                   	         53	         53	              4
  update checkout lines with reservations                                 	        128	        128	             52

# saleor.graphql.checkoutbenchmark homepage
  test name                                                               	left count 	right count	duplicate count
  ------------------------------------------------------------------------	-----------	-----------	---------------
  user checkout details                                                   	         41	         41	              2

# saleor.graphql.discountbenchmark sales
  test name                                                               	left count 	right count	duplicate count
  ------------------------------------------------------------------------	-----------	-----------	---------------
  sales query with channel slug                                           	         19	         19	              0
  sales query withot channel slug                                         	         18	         18	              0

# saleor.graphql.discountbenchmark vouchers
  test name                                                               	left count 	right count	duplicate count
  ------------------------------------------------------------------------	-----------	-----------	---------------
  vouchers query with channel slug                                        	         19	         19	              0
  vouchers query withot channel slug                                      	         18	         18	              0

# saleor.graphql.giftcardbenchmark gift card mutations
  test name                                                               	left count 	right count	duplicate count
  ------------------------------------------------------------------------	-----------	-----------	---------------
  create never expiry gift card                                           	         20	         20	              4
  gift card bulk activate by staff                                        	         13	         13	              3
  update gift card                                                        	         16	         16	              3

# saleor.graphql.giftcardbenchmark gift card queries
  test name                                                               	left count 	right count	duplicate count
  ------------------------------------------------------------------------	-----------	-----------	---------------
  filter gift cards by products                                           	         12	         12	              3
  filter gift cards by tag                                                	         10	         10	              3
  filter gift cards by used by user                                       	         11	         11	              3
  query gift card details                                                 	         11	         11	              3
  query gift cards                                                        	         10	         10	              3

# saleor.graphql.orderbenchmark fulfillment refund and return products
  test name                                                               	left count 	right count	duplicate count
  ------------------------------------------------------------------------	-----------	-----------	---------------
  fulfillment refund products order lines                                 	         56	         56	              2
  fulfillment return products order lines                                 	        109	        109	             14

# saleor.graphql.orderbenchmark order
  test name                                                               	left count 	right count	duplicate count
  ------------------------------------------------------------------------	-----------	-----------	---------------
  staff multiple draft orders                                             	        400	        400	            306
  staff multiple orders                                                   	        150	        150	             74
  staff order details                                                     	         44	         44	              5
  user order details                                                      	         42	         42	              6

# saleor.graphql.orderbenchmark order fulfill
  test name                                                               	left count 	right count	duplicate count
  ------------------------------------------------------------------------	-----------	-----------	---------------
  order fulfill                                                           	         17	         17	              3
  order fulfill with gift cards                                           	         17	         17	              3

# saleor.graphql.pagebenchmark page type
  test name                                                               	left count 	right count	duplicate count
  ------------------------------------------------------------------------	-----------	-----------	---------------
  page types for federation query count                                   	          3	          3	              0
  query page type                                                         	         19	         19	              7
  query page types                                                        	         25	         25	             12

# saleor.graphql.productbenchmark category
  test name                                                               	left count 	right count	duplicate count
  ------------------------------------------------------------------------	-----------	-----------	---------------
  categories children                                                     	          3	          3	              0
  categories for federation query count                                   	          3	          3	              0
  category delete                                                         	        109	        109	             27
  category view                                                           	         26	         26	              1

# saleor.graphql.productbenchmark collection
  test name                                                               	left count 	right count	duplicate count
  ------------------------------------------------------------------------	-----------	-----------	---------------
  collection add products                                                 	         44	         44	             10
  collection bulk delete                                                  	         51	         51	             10
  collection view                                                         	          6	          6	              0
  collections for federation query count                                  	          7	          7	              2
  create collection                                                       	         47	         47	             12
  delete collection                                                       	         46	         46	             11
  remove products from collection                                         	         41	         41	             10
  retrieve collection channel listings                                    	          5	          5	              0

# saleor.graphql.productbenchmark homepage
  test name                                                               	left count 	right count	duplicate count
  ------------------------------------------------------------------------	-----------	-----------	---------------
  report product sales                                                    	         10	         10	              3
  retrieve product list                                                   	          4	          4	              0

# saleor.graphql.productbenchmark product
  test name                                                               	left count 	right count	duplicate count
  ------------------------------------------------------------------------	-----------	-----------	---------------
  filter products by attributes                                           	          9	          9	              0
  filter products by boolean attributes                                   	         19	         19	              0
  filter products by gift card                                            	         11	         11	              1
  filter products by numeric attributes                                   	         18	         18	              0
  product create                                                          	         70	         70	              3
  product details                                                         	         32	         32	              0
  product translations                                                    	          5	          5	              0
  products for federation query count                                     	          6	          6	              2
  products media for federation query count                               	          3	          3	              0
  products types for federation query count                               	          2	          2	              0
  retrieve channel listings                                               	         17	         17	              0
  retrieve product attributes                                             	          9	          9	              0
  retrieve product images                                                 	          4	          4	              0
  retrieve product media                                                  	          4	          4	              0
  retrive products with product types and attributes                      	          7	          7	              0
  update product                                                          	         46	         46	              4

# saleor.graphql.productbenchmark product variant channel listing update
  test name                                                               	left count 	right count	duplicate count
  ------------------------------------------------------------------------	-----------	-----------	---------------
  variant channel listing update                                          	         50	         50	              5

# saleor.graphql.productbenchmark variant
  test name                                                               	left count 	right count	duplicate count
  ------------------------------------------------------------------------	-----------	-----------	---------------
  product variant bulk create                                             	         59	         59	              2
  product variant create                                                  	         75	         75	              6
  products variants for federation query count                            	          6	          6	              2
  retrieve variant list                                                   	         28	         28	              2
  update product variant                                                  	         79	         79	             12

# saleor.graphql.productbenchmark variant stocks
  test name                                                               	left count 	right count	duplicate count
  ------------------------------------------------------------------------	-----------	-----------	---------------
  product variants stocks create                                          	         27	         27	              5
  product variants stocks create with single webhook called               	         25	         25	              5
  product variants stocks delete                                          	         27	         27	              5
  product variants stocks delete with out of stock webhook many calls     	         23	         23	              3
  product variants stocks update                                          	         29	         29	              5
  query product variants stocks                                           	         10	         10	              0

# saleor.graphql.producttest product sorting attributes
  test name                                                               	left count 	right count	duplicate count
  ------------------------------------------------------------------------	-----------	-----------	---------------
  sort product not having attribute data                                  	         23	         23	              0

# saleor.graphql.shippingbenchmark shipping methods
  test name                                                               	left count 	right count	duplicate count
  ------------------------------------------------------------------------	-----------	-----------	---------------
  vouchers query with channel slug                                        	         10	         10	              0
  vouchers query without channel slug                                     	          9	          9	              0

# saleor.graphql.shopbenchmark homepage
  test name                                                               	left count 	right count	duplicate count
  ------------------------------------------------------------------------	-----------	-----------	---------------
  retrieve shop                                                           	          6	          6	              0

@codecov
Copy link

codecov bot commented Nov 26, 2021

Codecov Report

Merging #8665 (4af780a) into main (1d6369f) will decrease coverage by 0.00%.
The diff coverage is n/a.

Impacted file tree graph

@@            Coverage Diff             @@
##             main    #8665      +/-   ##
==========================================
- Coverage   93.02%   93.01%   -0.01%     
==========================================
  Files         499      500       +1     
  Lines       38265    38371     +106     
  Branches     4422     6527    +2105     
==========================================
+ Hits        35596    35692      +96     
- Misses       1782     1791       +9     
- Partials      887      888       +1     
Impacted Files Coverage Δ
saleor/graphql/views.py 87.41% <ø> (ø)
saleor/graphql/order/filters.py 79.31% <0.00%> (-9.27%) ⬇️
...aleor/payment/gateways/dummy_credit_card/plugin.py 86.95% <0.00%> (-8.70%) ⬇️
saleor/graphql/core/fields.py 96.80% <0.00%> (-0.57%) ⬇️
saleor/graphql/product/filters.py 89.50% <0.00%> (-0.33%) ⬇️
saleor/plugins/manager.py 93.01% <0.00%> (ø)
saleor/graphql/app/types.py 100.00% <0.00%> (ø)
saleor/graphql/account/utils.py 98.55% <0.00%> (ø)
saleor/graphql/account/schema.py 100.00% <0.00%> (ø)
saleor/graphql/shipping/types.py 89.54% <0.00%> (ø)
... and 16 more

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 1d6369f...4af780a. Read the comment docs.

@LeOndaz
Copy link
Contributor Author

LeOndaz commented Nov 27, 2021

Kindly note that I wanted to access the plugin configuration in the schema of the GraphQLView above, and the only 2 way I could use are, either extend the GraphQLView and let it accept new parameters, or monkey-patch it on the request on the fly

    def webhook(self, request, path, previous_value):
        view = GraphQLView.as_view(schema=schema)
        request.plugin_config = self.configuration   # or whatever needed
        return view(request)

And this lets me think of, why don't we return the request.app that was removed in an issue before and set it to the requestor plugin instance, or app URL?, same as:

    def webhook(self, request, path, previous_value):
        view = GraphQLView.as_view(schema=schema)
        request.app = self
        return view(request)

@LeOndaz
Copy link
Contributor Author

LeOndaz commented Nov 27, 2021

@korycins @patrys @maarcingebala

Not sure I know someone else here, can we discuss this?

@patrys
Copy link
Member

patrys commented Dec 6, 2021

@LeOndaz, could you start by describing the use case/problem you're trying to solve? I'm not sure I understand why that change is beneficial.

@LeOndaz
Copy link
Contributor Author

LeOndaz commented Dec 6, 2021

@LeOndaz, could you start by describing the use case/problem you're trying to solve? I'm not sure I understand why that change is beneficial.

Hello @patrys , it's nice to hear from you again, let me elaborate

Plugins extend the core functionality, but webhook are restful, we can make the plugins expose a GraphQL endpoint as well, but we do need the plugin to use the same playground instead of copying it around just to edit the content security policy, plugins at the moment can't use playground.html because content security policy doesn't allow urls other than api_url and this update adds support for plugin urls as well.

saleor/graphql/views.py Outdated Show resolved Hide resolved
@patrys
Copy link
Member

patrys commented Dec 6, 2021

@LeOndaz, I get it. That makes sense.

@LeOndaz
Copy link
Contributor Author

LeOndaz commented Dec 6, 2021

@patrys So basically, we can't render the template from any urls other than {{ api_uri }}, but plugins need to render it because some plugins may want to add a GraphQL playground on their own then we federate all of them at the end, this is the first step towards allowing plugins to be GraphQL services, another step was suggested above and here's a demo:

The request.app is not related to this specific PR, but here's a use case

/plugins/my.plugin/

initiateOAuth2(provider: Google) {
    url
    state
}

OAuth2Callback(provider: Google, code: "code", state: "statex") {
     user {
        id
     }
}

and the implementation is

class initiateOAuth2Mutation(...):
    def perform_mutation(self, ...)
        # how can I know get the plugin's configuration here? I want client_id and client_secret of google to be able to use oauth2 
        # here lies the benefit of request.app again, 

@LeOndaz
Copy link
Contributor Author

LeOndaz commented Dec 6, 2021

@patrys can you also give me your opinion about the request.app thing?

@LeOndaz
Copy link
Contributor Author

LeOndaz commented Dec 6, 2021

@patrys not sure why it is failing, I'll appreciate some help and how can this be merged

@LeOndaz LeOndaz requested a review from patrys December 6, 2021 23:25
@patrys patrys requested review from maarcingebala and removed request for patrys December 7, 2021 15:23
{"api_url": request.build_absolute_uri(str(API_PATH))},
{
"api_url": request.build_absolute_uri(str(API_PATH)),
"plugins_url": request.build_absolute_uri("/plugins/"),
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good but shouldn't we have this URL defined in urls.py and use the reverse function here? I'm not sure as there is no view /plugins/; there is the plugins/(?P<plugin_id>[.0-9A-Za-z_\-]+)/ named URL but it requires the ID.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

reverse won't work because it's not defined in url.py, but build_absolute_uri works because it just takes a path and returns it relative to the current host

P.S: This is tested

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍

@maarcingebala maarcingebala merged commit 7468c40 into saleor:main Dec 8, 2021
@LeOndaz
Copy link
Contributor Author

LeOndaz commented Dec 8, 2021

@maarcingebala
Should I PR adding request.app again?

look closely at my example above, the schema is rendered correctly but it has no access to the plugin configuration

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

Successfully merging this pull request may close these issues.

None yet

4 participants