Skip to content

Commit bff2776

Browse files
authored
Update repo with the latest-and-greatest GraphQL.NET/CI/Code Style (#254)
1 parent e3f95d1 commit bff2776

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

54 files changed

+1619
-1688
lines changed

.editorconfig

Lines changed: 28 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,7 @@ csharp_style_var_elsewhere = true:suggestion
9494
# C# code style settings - Expression-bodied members
9595
# https://docs.microsoft.com/en-us/visualstudio/ide/editorconfig-language-conventions?view=vs-2019#expression-bodied-members
9696
csharp_style_expression_bodied_methods = when_on_single_line:suggestion
97-
csharp_style_expression_bodied_constructors = false:suggestion
97+
csharp_style_expression_bodied_constructors = false:warning
9898
csharp_style_expression_bodied_operators = when_on_single_line:warning
9999
csharp_style_expression_bodied_properties = when_on_single_line:warning
100100
csharp_style_expression_bodied_indexers = when_on_single_line:warning
@@ -131,8 +131,8 @@ csharp_style_unused_value_assignment_preference = discard_variable:suggestion
131131

132132
# C# code style - Index and range preferences
133133
# https://docs.microsoft.com/en-us/visualstudio/ide/editorconfig-language-conventions?view=vs-2019#index-and-range-preferences
134-
csharp_style_prefer_index_operator = true:warning
135-
csharp_style_prefer_range_operator = true:warning
134+
csharp_style_prefer_index_operator = true:suggestion
135+
csharp_style_prefer_range_operator = true:suggestion
136136

137137
# C# code style - Miscellaneous preferences
138138
# https://docs.microsoft.com/en-us/visualstudio/ide/editorconfig-language-conventions?view=vs-2019#miscellaneous-preferences
@@ -196,6 +196,9 @@ csharp_space_between_square_brackets = false
196196
csharp_preserve_single_line_blocks = true
197197
csharp_preserve_single_line_statements = false
198198

199+
# C# formatting settings - Namespace options
200+
csharp_style_namespace_declarations = file_scoped:suggestion
201+
199202
########## name all private fields using camelCase with underscore prefix ##########
200203
# https://docs.microsoft.com/en-us/visualstudio/ide/editorconfig-naming-conventions?view=vs-2019
201204
# dotnet_naming_rule.<namingRuleTitle>.symbols = <symbolTitle>
@@ -258,4 +261,25 @@ dotnet_naming_style.end_in_async_style.required_suffix = Async
258261
dotnet_naming_rule.async_methods_end_in_async.severity = warning
259262

260263
# Remove unnecessary import https://docs.microsoft.com/en-us/dotnet/fundamentals/code-analysis/style-rules/ide0005
261-
dotnet_diagnostic.IDE0005.severity = warning
264+
dotnet_diagnostic.IDE0005.severity = error
265+
266+
# Enforce formatting https://docs.microsoft.com/en-us/dotnet/fundamentals/code-analysis/style-rules/formatting-rules#rule-id-ide0055-fix-formatting
267+
dotnet_diagnostic.IDE0055.severity = error
268+
269+
# https://github.com/JosefPihrt/Roslynator/blob/master/docs/analyzers/RCS0060.md
270+
dotnet_diagnostic.RCS0060.severity = warning
271+
roslynator_blank_line_after_file_scoped_namespace_declaration = true
272+
273+
# https://github.com/JosefPihrt/Roslynator/blob/main/docs/analyzers/RCS1080.md
274+
dotnet_diagnostic.RCS1080.severity = warning
275+
276+
# ConfigureAwait https://github.com/JosefPihrt/Roslynator/blob/master/docs/analyzers/RCS1090.md
277+
dotnet_diagnostic.RCS1090.severity = warning
278+
roslynator_configure_await = true
279+
280+
# https://github.com/JosefPihrt/Roslynator/blob/master/docs/analyzers/RCS1102.md
281+
# TODO: NullabilityInfo issue in Patching.cs in internal class SR
282+
dotnet_diagnostic.RCS1102.severity = suggestion
283+
284+
# https://github.com/JosefPihrt/Roslynator/blob/master/docs/analyzers/RCS1194.md
285+
dotnet_diagnostic.RCS1194.severity = suggestion

.github/dependabot.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
1+
# https://docs.github.com/en/code-security/dependabot/dependabot-version-updates/configuration-options-for-the-dependabot.yml-file
12
version: 2
23
updates:
4+
35
- package-ecosystem: "nuget"
46
directory: "/"
57
schedule:

.github/labeler.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ CI:
66
- .github/workflows/**/*
77
- .github/dependabot.yml
88
- .github/labeler.yml
9+
- .github/codecov.yml
910

1011
code style:
1112
- .editorconfig

.github/workflows/build.yml

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ on:
99
- develop
1010
paths:
1111
- src/**
12+
- .github/workflows/**
1213

1314
env:
1415
DOTNET_NOLOGO: true
@@ -19,10 +20,10 @@ jobs:
1920
runs-on: ubuntu-latest
2021
steps:
2122
- uses: actions/checkout@v3
22-
- name: Setup .NET Core SDK
23+
- name: Setup .NET SDK
2324
uses: actions/setup-dotnet@v3
2425
with:
25-
dotnet-version: "6.0.x"
26+
dotnet-version: "7.0.x"
2627
source-url: https://nuget.pkg.github.com/graphql-dotnet/index.json
2728
env:
2829
NUGET_AUTH_TOKEN: ${{secrets.GITHUB_TOKEN}}

.github/workflows/codeql-analysis.yml

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,10 @@ jobs:
1616
- name: Checkout source
1717
uses: actions/checkout@v3
1818

19-
- name: Setup .NET Core SDK
19+
- name: Setup .NET SDK
2020
uses: actions/setup-dotnet@v3
2121
with:
22-
dotnet-version: "6.0.x"
22+
dotnet-version: "7.0.x"
2323
source-url: https://nuget.pkg.github.com/graphql-dotnet/index.json
2424
env:
2525
NUGET_AUTH_TOKEN: ${{secrets.GITHUB_TOKEN}}
@@ -32,10 +32,11 @@ jobs:
3232

3333
- name: Install dependencies
3434
working-directory: src
35-
run: dotnet restore -p:GraphQLTestVersion=5.1.1
35+
run: dotnet restore
3636

37-
- name: Build solution
38-
working-directory: src
37+
- name: Build CodeQL solution
38+
# https://docs.github.com/en/code-security/code-scanning/automatically-scanning-your-code-for-vulnerabilities-and-errors/troubleshooting-the-codeql-workflow#reduce-the-amount-of-code-being-analyzed-in-a-single-workflow
39+
working-directory: codeql
3940
run: dotnet build --no-restore
4041

4142
- name: Perform CodeQL Analysis

.github/workflows/format.yml

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
name: Check formatting
2+
3+
on:
4+
pull_request:
5+
branches:
6+
- master
7+
- develop
8+
paths:
9+
- src/**
10+
- .github/workflows/**
11+
12+
env:
13+
DOTNET_NOLOGO: true
14+
DOTNET_CLI_TELEMETRY_OPTOUT: true
15+
16+
jobs:
17+
format:
18+
runs-on: ubuntu-latest
19+
steps:
20+
- name: Checkout source
21+
uses: actions/checkout@v3
22+
- name: Setup .NET SDK
23+
uses: actions/setup-dotnet@v3
24+
with:
25+
dotnet-version: 7.0.x
26+
source-url: https://nuget.pkg.github.com/graphql-dotnet/index.json
27+
env:
28+
NUGET_AUTH_TOKEN: ${{secrets.GITHUB_TOKEN}}
29+
- name: Install dependencies
30+
working-directory: src
31+
run: dotnet restore
32+
- name: Check formatting
33+
working-directory: src
34+
run: |
35+
dotnet format --no-restore --verify-no-changes --severity warn || (echo "Run 'dotnet format' to fix issues" && exit 1)

.github/workflows/publish.yml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,10 +27,10 @@ jobs:
2727
version="${github_ref:10}"
2828
echo version=$version
2929
echo "version=$version" >> $GITHUB_ENV
30-
- name: Setup .NET Core SDK
30+
- name: Setup .NET SDK
3131
uses: actions/setup-dotnet@v3
3232
with:
33-
dotnet-version: "6.0.x"
33+
dotnet-version: "7.0.x"
3434
source-url: https://api.nuget.org/v3/index.json
3535
env:
3636
NUGET_AUTH_TOKEN: ${{secrets.NUGET_AUTH_TOKEN}}
@@ -48,7 +48,7 @@ jobs:
4848
with:
4949
name: Nuget packages
5050
path: |
51-
out/*
51+
src/out/*
5252
- name: Publish Nuget packages to Nuget registry
5353
working-directory: src
5454
run: dotnet nuget push "out/*" -k ${{secrets.NUGET_AUTH_TOKEN}}

.github/workflows/test.yml

Lines changed: 27 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -23,53 +23,60 @@ env:
2323

2424
jobs:
2525
test:
26-
runs-on: ${{ matrix.os }}
2726
strategy:
2827
matrix:
29-
os:
30-
- ubuntu-latest
31-
- windows-latest
28+
os: [ubuntu-latest, windows-latest]
3229
graphqlversion:
33-
- 5.1.1
34-
- 5.2.0
30+
- 7.0.0
31+
- 7.1.1
32+
- 7.2.2
33+
name: ${{ matrix.os }}/${{ matrix.graphqlversion }}
34+
runs-on: ${{ matrix.os }}
3535
steps:
3636
- name: Checkout source
3737
uses: actions/checkout@v3
38-
- name: Setup .NET Core SDKs
38+
- name: Setup .NET SDKs
3939
uses: actions/setup-dotnet@v3
4040
with:
4141
dotnet-version: |
4242
3.1.x
4343
5.0.x
4444
6.0.x
45+
7.0.x
4546
source-url: https://nuget.pkg.github.com/graphql-dotnet/index.json
4647
env:
4748
NUGET_AUTH_TOKEN: ${{secrets.GITHUB_TOKEN}}
48-
- name: Disable MSVS Nuget Source # see https://github.com/graphql-dotnet/graphql-dotnet/issues/2422
49-
if: ${{ startsWith(matrix.os, 'windows') }}
50-
run: dotnet nuget disable source 'Microsoft Visual Studio Offline Packages'
5149
- name: Install dependencies with GraphQL version ${{ matrix.graphqlversion }}
5250
working-directory: src
5351
run: dotnet restore -p:GraphQLTestVersion=${{ matrix.graphqlversion }}
54-
- name: Check formatting
55-
if: ${{ startsWith(matrix.os, 'ubuntu') }}
56-
working-directory: src
57-
run: |
58-
dotnet format --no-restore --verify-no-changes --severity warn || (echo "Run 'dotnet format' to fix issues" && exit 1)
5952
- name: Build solution [Release]
53+
if: ${{ startsWith(matrix.os, 'ubuntu') }}
6054
working-directory: src
6155
run: dotnet build --no-restore -c Release -p:GraphQLTestVersion=${{ matrix.graphqlversion }}
6256
- name: Build solution [Debug]
6357
working-directory: src
64-
run: dotnet build --no-restore -p:GraphQLTestVersion=${{ matrix.graphqlversion }}
65-
- name: Test solution [Debug]
58+
run: dotnet build --no-restore -c Debug -p:GraphQLTestVersion=${{ matrix.graphqlversion }}
59+
- name: Test solution [Debug] with code coverage
60+
if: ${{ startsWith(matrix.os, 'ubuntu') }}
61+
working-directory: src
62+
run: >
63+
dotnet test
64+
--no-restore
65+
--no-build
66+
--collect "XPlat Code Coverage"
67+
--results-directory .coverage
68+
--
69+
DataCollectionRunSettings.DataCollectors.DataCollector.Configuration.Format=opencover
70+
DataCollectionRunSettings.DataCollectors.DataCollector.Configuration.Exclude=[BasicSample]*,[Harness]*
71+
- name: Test solution [Debug] without code coverage
72+
if: ${{ startsWith(matrix.os, 'windows') }}
6673
working-directory: src
67-
run: dotnet test --no-restore --no-build -p:CollectCoverage=true
74+
run: dotnet test --no-restore --no-build
6875
- name: Upload coverage to codecov
6976
if: ${{ startsWith(matrix.os, 'ubuntu') }}
70-
uses: codecov/codecov-action@v3.1.1
77+
uses: codecov/codecov-action@v3
7178
with:
72-
files: .coverage/GraphQL.Authorization.Tests/coverage.net5.opencover.xml
79+
files: 'src/.coverage/**/coverage.opencover.xml'
7380

7481
buildcheck:
7582
needs:

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,3 +8,5 @@
88

99
[Oo]bj/
1010
[Bb]in/
11+
12+
*.received.txt

README.md

Lines changed: 33 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,18 @@
11
# GraphQL Authorization
22

3-
[![Join the chat at https://gitter.im/graphql-dotnet/graphql-dotnet](https://badges.gitter.im/Join%20Chat.svg)](https://gitter.im/graphql-dotnet/graphql-dotnet?utm_source=badge&utm_medium=badge&utm_campaign=pr-badge&utm_content=badge)
4-
5-
[![Run code tests](https://github.com/graphql-dotnet/authorization/actions/workflows/test.yml/badge.svg)](https://github.com/graphql-dotnet/authorization/actions/workflows/test.yml)
6-
[![Build artifacts](https://github.com/graphql-dotnet/authorization/actions/workflows/build.yml/badge.svg)](https://github.com/graphql-dotnet/authorization/actions/workflows/build.yml)
7-
[![Publish release](https://github.com/graphql-dotnet/authorization/actions/workflows/publish.yml/badge.svg)](https://github.com/graphql-dotnet/authorization/actions/workflows/publish.yml)
8-
[![CodeQL analysis](https://github.com/graphql-dotnet/authorization/actions/workflows/codeql-analysis.yml/badge.svg)](https://github.com/graphql-dotnet/authorization/actions/workflows/codeql-analysis.yml)
9-
3+
[![License](https://img.shields.io/github/license/graphql-dotnet/authorization)](LICENSE.md)
104
[![codecov](https://codecov.io/gh/graphql-dotnet/authorization/branch/master/graph/badge.svg?token=TODO)](https://codecov.io/gh/graphql-dotnet/authorization)
11-
[![Total alerts](https://img.shields.io/lgtm/alerts/g/graphql-dotnet/authorization.svg?logo=lgtm&logoWidth=18)](https://lgtm.com/projects/g/graphql-dotnet/authorization/alerts/)
12-
[![Language grade: C#](https://img.shields.io/lgtm/grade/csharp/g/graphql-dotnet/authorization.svg?logo=lgtm&logoWidth=18)](https://lgtm.com/projects/g/graphql-dotnet/authorization/context:csharp)
5+
[![Nuget](https://img.shields.io/nuget/dt/GraphQL.Authorization)](https://www.nuget.org/packages/GraphQL.Authorization)
6+
[![Nuget](https://img.shields.io/nuget/v/GraphQL.Authorization)](https://www.nuget.org/packages/GraphQL.Authorization)
7+
[![GitHub Release Date](https://img.shields.io/github/release-date/graphql-dotnet/authorization?label=released)](https://github.com/graphql-dotnet/authorization/releases)
8+
[![GitHub commits since latest release (by date)](https://img.shields.io/github/commits-since/graphql-dotnet/authorization/latest?label=new+commits)](https://github.com/graphql-dotnet/authorization/commits/master)
9+
![Size](https://img.shields.io/github/repo-size/graphql-dotnet/authorization)
1310

11+
[![GitHub contributors](https://img.shields.io/github/contributors/graphql-dotnet/authorization)](https://github.com/graphql-dotnet/authorization/graphs/contributors)
1412
![Activity](https://img.shields.io/github/commit-activity/w/graphql-dotnet/authorization)
1513
![Activity](https://img.shields.io/github/commit-activity/m/graphql-dotnet/authorization)
1614
![Activity](https://img.shields.io/github/commit-activity/y/graphql-dotnet/authorization)
1715

18-
![Size](https://img.shields.io/github/repo-size/graphql-dotnet/authorization)
19-
2016
A toolset for authorizing access to graph types for [GraphQL.NET](https://github.com/graphql-dotnet/graphql-dotnet).
2117

2218
Provides the following packages:
@@ -30,47 +26,61 @@ Note that GitHub requires authentication to consume the feed. See [here](https:/
3026

3127
# Usage
3228

33-
- Register the authorization classes in your DI container - `IAuthorizationEvaluator`, `AuthorizationSettings`, and the `AuthorizationValidationRule`.
34-
- Provide a custom `UserContext` class that implements `IProvideClaimsPrincipal`.
29+
- Register the authorization classes in your DI container - call `AddAuthorization` on the provided `IGraphQLBuilder` inside `AddGraphQL` extension method.
30+
- Provide the `ClaimsPrincipal` through `ExecutionOptions.User`.
3531
- Add policies to the `AuthorizationSettings`.
36-
- Apply a policy to a GraphType or Field (both implement `IProvideMetadata`):
37-
- using `AuthorizeWith(string policy)` extension method
38-
- or with `GraphQLAuthorize` attribute if using Schema + Handler syntax.
32+
- Apply a policy to a GraphType or Field - both implement `IProvideMetadata`:
33+
- using `AuthorizeWithPolicy(string policy)` extension method
34+
- or with `AuthorizeAttribute` attribute if using Schema + Handler syntax.
3935
- The `AuthorizationValidationRule` will run and verify the policies based on the registered policies.
4036
- You can write your own `IAuthorizationRequirement`.
4137

38+
# Limitations
39+
40+
`@skip` and `@include` directives are ignored; all selected fields of the selected operation will
41+
be checked for authentication requirements, including referenced fragments. (Other operations
42+
in the same document will correctly be skipped.)
43+
44+
This authorization framework only supports policy-based authorization. It does not support role-based authorization, or the
45+
`[AllowAnonymous]` attribute/extension, or the `[Authorize]` attribute/extension indicating authorization is required
46+
but without specifying a policy. It also does not integrate with ASP.NET Core's authorization framework.
47+
48+
The [GraphQL.Server](https://www.github.com/graphql-dotnet/server) repository contains an authorization rule which has the above
49+
missing features, intended for use with ASP.NET Core. It may also be tailored with custom authentication code if desired, rather than
50+
relying on ASP.NET Core's authentication framework.
51+
4252
# Examples
4353

4454
1. Fully functional basic [Console sample](src/BasicSample/Program.cs).
4555

4656
2. Fully functional [ASP.NET Core sample](src/Harness/Program.cs).
4757

48-
3. GraphType first syntax - use `AuthorizeWith` extension method on `IGraphType` or `IFieldType`.
58+
3. GraphType first syntax - use `AuthorizeWithPolicy` extension method on `IGraphType` or `IFieldType`.
4959

5060
```csharp
5161
public class MyType : ObjectGraphType
5262
{
5363
public MyType()
5464
{
55-
this.AuthorizeWith("AdminPolicy");
56-
Field<StringGraphType>("name").AuthorizeWith("SomePolicy");
65+
this.AuthorizeWithPolicy("AdminPolicy");
66+
Field<StringGraphType>("name").AuthorizeWithPolicy("SomePolicy");
5767
}
5868
}
5969
```
6070

61-
4. Schema first syntax - use `GraphQLAuthorize` attribute on type, method or property.
71+
4. Schema first syntax - use `AuthorizeAttribute` attribute on type, method or property.
6272

6373
```csharp
64-
[GraphQLAuthorize("MyPolicy")]
74+
[Authorize("MyPolicy")]
6575
public class MutationType
6676
{
67-
[GraphQLAuthorize("AnotherPolicy")]
77+
[Authorize("AnotherPolicy")]
6878
public async Task<string> CreateSomething(MyInput input)
6979
{
7080
return await SomeMethodAsync(input);
7181
}
7282

73-
[GraphQLAuthorize("SuperPolicy")]
83+
[Authorize("SuperPolicy")]
7484
public string SomeProperty => Guid.NewGuid().ToString();
7585
}
7686
```

0 commit comments

Comments
 (0)